poj 1083 Moving Tables

来源:互联网 发布:文件打开软件 编辑:程序博客网 时间:2024/06/06 02:46
Moving Tables
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29479 Accepted: 9803

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


解题思路:本题咋一看,还以为是个贪心,其实还有更简单的办法。

用数组d[401]记录每个房间前的走廊被用过多少次,则答案即为d[i]*10;每扫描一个点将数组这个范围内的值加1,注意,范围的确定,若下限为偶数,则下限需减一,若上限为奇数,则上限需加1,例如,s=4,t
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 500;#define Max(a,b) a>b?a:b;#define Min(a,b) a>b?b:a;int t,n;int vis[maxn];int main(){    scanf("%d",&t);    while(t--){        scanf("%d",&n);        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++){            int a,b;            scanf("%d %d",&a,&b);            int c,d;            c = Max(a,b);            d = Min(a,b);            if(d%2==0)                d--;            if(c%2)                c++;            for(int j=d;j<=c;j++)                vis[j]++;        }        int ans = -1;        for(int i=0;i<maxn;i++){            //cout<<vis[i]<<endl;            if(ans < vis[i])                ans = vis[i];        }        printf("%d\n",ans*10);    }    return 0;}

错误代码:有一组数据过不了。

102 34 56 78 910 1112 1314 1516 1718 1920 21
应该为20,程序为10.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 500;int t,n;int vis[maxn];pair<int,int> itv[maxn];bool cmp(pair<int,int> a,pair<int,int> b){    return a.first<b.first;}int main(){    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(int i=0;i<n;i++){            vis[i] = 0;            int a;            int b;            scanf("%d %d",&a,&b);            itv[i] = make_pair(a,b);        }        sort(itv,itv+n,cmp);        int sum = 0;        for(int i=0;i<n;i++){            if(vis[i]==0){                vis[i] = 1;                sum += 10;            }            int a = i,b=i+1;            while(b!=n){                if(vis[b]==0 && itv[b].first>itv[a].second){                        vis[b] = 1;                        a = b;                        b = a+1;                }                else                    b++;            }        }        printf("%d\n",sum);    }    return 0;}


0 0