现在的位置: 首页 > 综合 > 正文

POJ_1083

2018年04月13日 ⁄ 综合 ⁄ 共 2749字 ⁄ 字号 评论关闭

一.题目

Moving Tables
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 25987
Accepted: 8660

Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 



The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only
one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the
part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the
possible cases and impossible cases of simultaneous moving. 




For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager's problem.

Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case begins with a line containing an integer N , 1 <= N <= 200, that represents the number
of tables to move. 
Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t each room number appears at most once in the N lines). From the 3 + N -rd 
line, the remaining test cases are listed in the same manner as above.

Output

The output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50 

Sample Output

10
20
30


二.解题技巧

    根据交流区的网友的提示,本题的结题思路是,统计每一个房间前面的区域被使用的次数,然后找出这些区域中使用次数最高的那个值,然后乘以10即可以得到最少需要的时间。因为,每一个区域在一次有冲突而无法并行的过程中,只能被占用一次,因此,被占用次数最多的那个区域,就表示最多需要串行处理的任务的个数,本题最少需要的时间即为最多需要串行的任务个数乘以一个任务的时间10。
    刚开始,我的解题思路是将每一个任务按照开始时间进行排序,然后找到与第一个任务时间不冲突的第一个任务i,则可以将从第一个到第i-1个任务划为一个整体,而将第i个到最后一个任务划为一个整体,这两个整体应该是可以并行计算的,但是,这样处理是错误的,因为在第一个整体中,可能有某一个任务就会和另一个整体的任务冲突了,因此,该想法是不可行的。

三.实现代码

    
#include <iostream>
#include <algorithm>
using namespace std;
void InitHist(int* Hist, int len)
{
    for (int Index = 0; Index < len; Index++)
    {
        Hist[Index] = 0;
    }
}

int findMax(int* Array, int Len)
{
    int Max = 0;
    for (int Index = 0; Index < Len; Index++)
    {
        if (Array[Index] > Max)
        {
            Max = Array[Index];
        }
    }
    return Max;
}

int main()
{
    int NumberOfTestCases = 0;
    int Size = 0;
    int Hist[200];
    int a = 0;
    int b = 0;

    cin >> NumberOfTestCases;

    for (int Index = 0; Index < NumberOfTestCases; Index++)
    {
        cin >> Size;
        InitHist(Hist, 200);
        for (int Index = 0; Index < Size; Index++)
        {
            cin >> a;
            cin >> b;

            if (b < a)
            {
                a += b;
                b = a - b;
                a = a - b;
            }

            a = (a + 1) / 2;
            b = (b + 1) / 2;
s
            for (int IndexOfArray = a; IndexOfArray <= b; IndexOfArray++)
            {
                Hist[IndexOfArray - 1]++;
            }
        }
        cout << findMax(Hist, 200) * 10 << endl;
    }

    return 0;
}



四.体会

    这种题目,虽然说是水题,但是,如果没有考虑到最大的串行任务的个数就是房间前面区域的使用次数的最大值的话,解起来也不是很容易的事,因此,要学会看到问题的本质,才是解题的关键。


版权所有,欢迎转载,转载请注明出处,谢谢微笑




抱歉!评论已关闭.