D - Moving Tables
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.
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. 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.
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
相对门的两个房间占用同一处走廊,所以例如有1——3和4——6移动方式时,它们是共享了同一段走廊的,即3号房间门前的走廊。
分析过程有如下图所示
所以当左端起点为偶数时,要将其中左端的起点再向左移动一位,
当右端终点为奇数时,要将右端的终点在向右移动一位。
计算每一个位置上最大走过的次数。
#include <cstdio>
#include <cstring>
#include<algorithm>
#define MAXN 505
using namespace std;
int main()
{
int Count[MAXN];
int i, j, testNum, n, L, R;
scanf("%d", &testNum);
while (testNum-- != 0)
{
scanf("%d", &n);
memset(Count, 0, sizeof(Count));
for (i = 0; i < n; i++)
{
scanf("%d %d", &L, &R);
if (L > R) swap(L,R);
if (L % 2 == 0)
Count[L-1]++;
if (R % 2 == 1)
Count[R+1]++;
for (j = L; j <= R; j++)
Count[j]++;
}
printf("%d\n", *max_element(Count,Count+MAXN)*10);
}
return 0;
}