## Wednesday, January 20, 2016

### HDU 1050 Moving Tables - POJ 1083

http://acm.hdu.edu.cn/showproblem.php?pid=1050
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. 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
http://hellojyj.iteye.com/blog/2094633

1. int T,n,rs,rt,ans,minute;
2. struct Room{
3.     int s;
4.     int t;
5.     bool operator <(const Room &rhs) const{
6.         return this->t<rhs.t;
7.     }
8. };
9. Room r[MAXN];
10. int main()
11. {
12.     scanf("%d",&T);
13.     while(T--){
14.         scanf("%d",&n);
15.         for(int i=0;i<n;i++){
16.             scanf("%d%d",&rs,&rt);
17.             rs = (rs+1)/2;
18.             rt = (rt+1)/2;
19.             if(rs>rt)swap(rs,rt);
20.             r[i].s = rs;
21.             r[i].t = rt;
22.         }
23.         sort(r,r+n);
24.         ans = 0;
25.         for(int i=0;i<n;i++){
26.             minute = 10;
27.             for(int j=i+1;j<n;j++){
28.                 if(r[i].t>=r[j].s){
29.                     minute +=10;
30.
31.                 }
32.             }
33.             if(minute>ans){
34.                 ans = minute;
35.             }
36.         }
37.         printf("%d\n",ans);
38.     }
39.     return 0;
40. }

1. int main(){
2.     int t;
3.     int cover[200];
4.     cin >> t;
5.     while( t-- ){
6.         memset(cover, 0,sizeof(cover));
7.         int n, s, f;
8.         cin >> n;
9.         while( n-- ){
10.             cin >> s >> f;
11.             s = (s - 1) / 2;        //相邻的奇数和偶数化成同一坐标
12.             f = (f - 1) / 2;
13.             if( s > f )
14.                 s ^= f ^= s ^= f;
15.             for(int i = s; i <= f; ++i)
16.                 cover[i]++;
17.         }
18.         int max = -1;
19.         for(int i = 0; i < 200; ++i){
20.             if( cover[i] > max )
21.                 max = cover[i];
22.         }
23.         cout << max * 10 << endl;
24.     }
25.     return 0;
26. }
http://www.conw.net/post-40.html

其实很简单，也并非是什么贪心，每个小区间加一个计数器，覆盖一次则计数器加1，最后找最大的计数器即可，代码如下：
题意：如下图形式给多个区间，求最少覆盖多少层，输出结果乘10。
如(1, 3)和(5, 7)则覆盖1层，(1, 5)和(3, 7)由于(3, 5)区间有重叠，所以覆盖了两层。
下图为示例几种区间覆盖：
public static void main(String[] args) throws NumberFormatException,
IOException {
int s; //s张桌子将搬动
int[][] m; //存放几组搬桌子起点终点的房间号
int[] corridors = new int[200]; //过道下标为k(对应的房间号为2k-1和2k+2),其值表示桌子经过过道的次数
int start; //开始的过道编号
int len; //搬桌子的距离
int max;
for (int i = 0; i < t; i++) {
Arrays.fill(corridors, 0); //对每组数据都重新初始化
m = new int[s][2];
for (int j = 0; j < s; j++) {
}
//遍历所有桌子，更新corridors数组中的值
for (int j = 0; j < s; j++) {
//将房间号转化为对应的过道号
if (m[j][0] % 2 == 0) {
m[j][0] = m[j][0] / 2 - 1;
} else {
m[j][0] = m[j][0] / 2;
}
if (m[j][1] % 2 == 0) {
m[j][1] = m[j][1] / 2 - 1;
} else {
m[j][1] = m[j][1] / 2;
}
len = Math.abs(m[j][0] - m[j][1]) + 1;
start = Math.min(m[j][0], m[j][1]);
for (int k = 0; k < len; k++) {
corridors[start + k]++;
}
}
//找到某个搬桌子过程中经过次数最多的过道号，找到此最大的次数
max = corridors[0];
for (int j = 1; j < 200; j++) {
if (corridors[j] > max) {
max = corridors[j];
}
}
System.out.println(max * 10);
}
}