本文主要是介绍201901-B - Moving Tables,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@201901冬训
B - Moving Tables
题目:
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
题目大意是在这个走廊两侧分别有200个房间,会给出一些数字对a,b表示要从a搬一张桌子到b,搬一次花费10分钟,搬的过程中a到b间(包括a,b处)的过道都是被占用的,不能再搬其他桌子。要求找到花费的最少时间。
题目给的是两侧,但其实奇数偶数都是一样的—从2到4搬桌子时,1到3的过道也是被占用的,所以读入时可以处理一下a,b,让a=(a+1)/2,b=(b+1)/2。
我一开始想到的办法是贪心,将数对排序,首先右侧升序,其次左侧降序,这样利用空间最大,然后不断遍历直到都搬完。
但最好的方法是:用一个数组记录第i间房间被占用的次数,然后求占用次数最大值即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
struct no{int a,b,c;
}s[202];
int n,ans=0;
int str(no aa,no bb)
{ if(aa.c!=bb.c)return aa.c<bb.c;if(aa.b!=bb.b)return aa.b<bb.b;return aa.a>bb.a;
}
int main()
{int t;cin>>t;while(t--){ans=0;cin>>n;for(int i=0;i<n;i++){cin>>s[i].a>>s[i].b;s[i].a=(s[i].a +1)/2; s[i].b=(s[i].b +1)/2;s[i].c=0;}int nn=n;sort(s,s+n,str); for(int i=0;i<n;i++)while(nn){ int lasa=0,lasb=0,de=0;for(int i=0;i<n;i++)if(s[i].c==0)if(s[i].a>lasb){s[i].c=1;lasa=s[i].a;lasb=s[i].b;de++;} nn=nn-de;ans++;} cout<<ans*10<<endl;}return 0;
}
这篇关于201901-B - Moving Tables的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!