Title Link: http://poj.org/problem?id=1083
As shown in the figure, there are 200 rooms on both sides of a corridor. Now, some pairs of rooms are given to exchange tables with each other, but the corridor can only be moved by one group at a time.
That is to say, if two handling processes are crossed, they cannot be carried at the same time, they should be carried in turn for 10 minutes at a time, and the minimum time to complete all the handling should be asked.
Idea: take the corridor of each left and right adjacent room as a statistical unit. When all the desks are moved, check how many times the corresponding statistical unit of each corridor is occupied.
Count the maximum number of times that the corridor of all the left and right adjacent rooms is occupied, that is, the transportation times arranged separately, multiplied by 10 is the total transportation time.
In addition, from and to are processed with - 1 mod 2 to correspond with array subscripts and facilitate processing.
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #include <cstdio> 6 using namespace std; 7 8 int main() 9 { 10 int t = 0; 11 cin >> t; 12 while (t-- > 0) 13 { 14 // One corridor between each two rooms, 200 corridors in total 15 int move[200]; 16 int n = 0; // Handling times 17 cin >> n; 18 memset(move, 0, sizeof(move)); 19 for (int i = 0; i < n; i++) 20 { 21 int from = 0, to = 0; 22 cin >> from >> to; 23 from = (from - 1) / 2; 24 to = (to - 1) / 2; 25 if (to < from) 26 { 27 swap(from, to); 28 } 29 for (int j = from; j <= to; j++) 30 { 31 move[j]++; 32 } 33 } 34 int max = 0; 35 for (int i = 0; i < 200; i++) 36 { 37 if (move[i] > max) 38 { 39 max = move[i]; 40 } 41 } 42 cout << max * 10 << "\n"; 43 } 44 return 0; 45 }