您的位置:首页 > 其它

练习1-a

2016-03-16 18:12 323 查看
编号:1000
Problem 

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.

 

[align=left]Input[/align]
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. 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 N+3-rd line, the remaining test cases are listed in the
same manner as above.
 

[align=left]Output[/align]
The output should contain the minimum time in minutes to complete the moving, one per line.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

10
20
30

A题意:ACM公司一条过道南北两面各200个房间,把桌子从一个房间搬到另一房间,每次每段过道只能搬一次桌子,用时10分钟。搬n次桌子,求最短时间。

思路:最开始对贪心算法理解错了,甚至是说没有理解,所以浪费了大把的时间。然后是我的思路,共有400间房间,但因为它们分别在走廊的两侧,所以在走廊同一位置的南北两间房间可也看做成一间,于是可以把这400间房间重新编号成200间。首先对输入各组的数据按小号房间号进行排列,在输入一组数据时要注意小房间在前,大房间在后,每个房间号减1除以2来生成新的房间号,之后便可以用贪心的算法进行求解了。在求解时设置了一个筛选变量,来对各组数据进行多次筛选。首先第一遍筛选,将没有相交的房间组进行筛选,并将筛选变量归零,以此来控制下次筛选时不对已筛选完成的房间组进行操作,用时10分钟;第二遍筛选将剩下的房间组再进行选择,继续选出不相交的房间组,用时10分钟,然后继续第三次筛选直至房间组全部筛选完成。最后输出,然后结束。

感想:第一次用贪心算法,还是很不熟悉,自己以为已经理解了在课上学的东西,但是在实际操作上却犯了难,还是反映了自己对贪心算法的理解并不深刻。第一遍读这个题时,感觉它很容易理解,所以写出的代码十分的无脑,之后又读了好几遍题目终于理解了一部分,但是有个难点随之出来,一直不会处理它,纠结了老长一段时间,就去参考了一下袁飞的代码,发现他设置了一个非常神奇的筛选变量,接着我又把课件拿出来复习了一下,之后算是才明白了怎样用贪心算法进行时间的安排。写出这段代码真是不容易,不过在这过程中也学到了很多东西,很是高兴,于是奖励了自己一块雪糕吃~

代码:(有参考)

# include <iostream>

# include <vector>

# include <algorithm>

using namespace std;

struct mi

{

    int be;

    int en;

    int nu;

    bool operator < (const mi &a) const

    {

        return be < a.be;

    }

};

int main()

{

    mi aa;

    vector <mi> aaa;

    int n;

    cin >> n;

    while (n--)

    {

        aaa.clear();

        int m;

        cin >> m;

        int a, b;

        while (m--)

        {

            cin >> a >> b;

            if (a > b)

            {

                int t = a;

                a = b;

                b = t;

            }

            aa.be = (a-1)/2;

            aa.en = (b-1)/2;

            aa.nu = 1;

            aaa.push_back(aa);

        }

        sort (aaa.begin(), aaa.end(), less<mi>());

        int time = 0;

        for (int i = 0; i < aaa.size(); i++)

        {

            if (aaa[i].nu == 0)

                continue;

            time ++;

            int pre = i;

            aaa[pre].nu = 0;

            for (int j = 0; j < aaa.size(); j++)

            {

                if (aaa[pre].en < aaa[j].be && aaa[j].nu !=0)

                {

                    aaa[j].nu = 0;

                    pre = j;

                }

            }

        }

            cout << 10*time <<endl;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: