1. 程式人生 > >Moving Tables

Moving Tables

ear sort struct write cst put system ref logs

Total Submissions: 32406 Accepted: 10831

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

題意:一條走廊,現在要移動桌子,從a房間移動到b房間,有交叉的地方不能同時移,即走廊的寬度只能容納一個桌子。
題解:首先將左右的房間進行壓縮,如1-2,壓縮成1,即相對的房間變成了一個房間。然後一條一條的掃,經過的房間就加上1.最後求出經過房間的最大次數。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6  
 7 const int maxn=400;
 8 
 9 int kase,n;
10 int map[maxn];
11 
12 int main()
13 {   cin>>kase;
14     while(kase--){
15         cin>>n;
16         memset(map,0,sizeof(map));
17         while(n--){
18             int a,b;
19             cin>>a>>b;
20             if(a>b) swap(a,b);
21             for(int i=(a+1)/2;i<=(b+1)/2;i++) map[i]++;
22         }
23         int ans=0;
24         for(int i=1;i<=maxn;i++) ans=max(ans,map[i]);
25         cout<<ans*10<<endl;
26     }
27     return 0;
28 }

這個改了好久,還是WA。。。不知道為什麽

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6  
 7 const int maxn=400;
 8 
 9 struct node{
10     int l,r;
11     bool operator<(const node& i)const{
12         if(l==i.l) return r<i.r;
13         return l<i.l;
14     }
15 }Room[maxn]; 
16 
17 int kase,n;
18 bool vis[maxn];
19 
20 void Solve(){
21     memset(vis,false,sizeof(vis));
22     vis[1]=true;
23     int ans=0,temp=1;
24     while(true){ 
25         for(int i=1;i<=n;i++){
26             if(vis[i]) continue;
27             if(Room[i].l-Room[temp].r>1) { vis[i]=true; temp=i; }
28             if(Room[i].l-Room[temp].r==1){ 
29                 if(Room[temp].l&1){ vis[i]=true; temp=i; }
30             }     
31         }
32 
33         ans++;
34         int cnt=0;
35         for(int i=1;i<=n;i++) if(!vis[i])
36         {   temp=i;
37             vis[i]=true;
38             cnt++;
39             break;
40         }
41         if(!cnt) break;
42     }
43     cout<<ans*10<<endl;
44 }
45 
46 int main()
47 {   cin>>kase;
48     while(kase--){
49         cin>>n;
50         for(int i=1;i<=n;i++){
51              scanf("%d%d",&Room[i].l,&Room[i].r);
52              if(Room[i].l>Room[i].r)
53                  swap(Room[i].l,Room[i].r);
54         }
55         sort(Room+1,Room+n+1);
56         //for(int i=1;i<=n;i++) cout<<Room[i].l<<" "<<Room[i].r<<endl;
57         Solve();
58     }
59     return 0;
60 } 

Moving Tables