欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1050 Moving tables

程序员文章站 2022-07-12 16:20:36
...

Moving Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18924    Accepted Submission(s): 6460


Problem Description

 

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.

HDU 1050 Moving tables
            
    
    博客分类: ACM 贪心区间重叠HDU1050 

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.

HDU 1050 Moving tables
            
    
    博客分类: ACM 贪心区间重叠HDU1050 

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
 

 

Source

 

 

 

 

Recommend

 

We have carefully selected several similar problems for you:  1052 1789 1045 1053 1257
 
心情:
博主真心要哭了,当我AC出这道题目的时候,满脸都是泪啊。快一天了,一直WA,WA,WA。。我真的像砸了笔记本,不过终于AC了,心情好了点,以后再碰到这样的题目,我不知道有没有勇气是AC。!!Orz!
错误思路:
用的是按照s升序排序,然后遍历每个区间,对于每个区间,与该区间重叠的所有区间总数记为sum[i],然后最大重叠数即为sum[]中的最大值。
错误分析:
比如说在某个大区间(1,200)内,里面有很多不重叠的小区间(1,30)(50,100)(120,150)如果按照上述方法,那么将会得到40分钟的答案,实际上大区间完成后,小区间是可以各自独立进行的,所以时间应该是20.
正确思路:
将排序方式改为以t升序排序,其他与原来思路保持一致
正确分析:
如果按照t升序将不会出现以上的小区间不重叠现象,只要与大区间重叠,小区间内必然相应的重叠
 
//贪心 HDU 1050
#include<cstdio>
#include<algorithm>
#define MAXN 210
using namespace std;
int T,n,rs,rt,ans,minute;
struct Room{
    int s;
    int t;
    bool operator <(const Room &rhs) const{
        return this->t<rhs.t;
    }
};
void swap(int &a,int &b){

    int temp = a;
    a = b;
    b =temp;

}
Room r[MAXN];
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&rs,&rt);
            rs = (rs+1)/2;
            rt = (rt+1)/2;
            if(rs>rt)swap(rs,rt);
            r[i].s = rs;
            r[i].t = rt;
        }
        sort(r,r+n);
        ans = 0;
        for(int i=0;i<n;i++){
            minute = 10;
            for(int j=i+1;j<n;j++){
                if(r[i].t>=r[j].s){
                    minute +=10;

                }
            }
            if(minute>ans){
                ans = minute;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


//测试数据
1
19
75 154
125 158
176 48
196 65
21 171
15 170
17 100
61 116
3 189
98 104
112 19
163 66
42 14
81 168
53 165
36 143
84 140
105 199
195 151

还有更加厉害的思路,就是统计每个房间被重叠的次数,最大时间就是最大房间重叠数。
#include <iostream>
#include <string.h>
using namespace std;

int main(){
    int t;
    int cover[200];
    cin >> t;
    while( t-- ){
        memset(cover, 0,sizeof(cover));
        int n, s, f;
        cin >> n;
        while( n-- ){
            cin >> s >> f;
            s = (s - 1) / 2;        //相邻的奇数和偶数化成同一坐标
            f = (f - 1) / 2;
            if( s > f )
                s ^= f ^= s ^= f;    
            for(int i = s; i <= f; ++i)
                cover[i]++; 
        }        
        int max = -1;            
        for(int i = 0; i < 200; ++i){
            if( cover[i] > max )
                max = cover[i];
        }        
        cout << max * 10 << endl;
    }
    return 0;
}