http://acm.hdu.edu.cn/showproblem.php?pid=1050
http://hellojyj.iteye.com/blog/2094633
就是统计每个房间被重叠的次数,最大时间就是最大房间重叠数。
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.
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
错误思路:
用的是按照s升序排序,然后遍历每个区间,对于每个区间,与该区间重叠的所有区间总数记为sum[i],然后最大重叠数即为sum[]中的最大值。
错误分析:
比如说在某个大区间(1,200)内,里面有很多不重叠的小区间(1,30)(50,100)(120,150)如果按照上述方法,那么将会得到40分钟的答案,实际上大区间完成后,小区间是可以各自独立进行的,所以时间应该是20.
正确思路:
将排序方式改为以t升序排序,其他与原来思路保持一致
正确分析:
如果按照t升序将不会出现以上的小区间不重叠现象,只要与大区间重叠,小区间内必然相应的重叠
- int T,n,rs,rt,ans,minute;
- struct Room{
- int s;
- int t;
- bool operator <(const Room &rhs) const{
- return this->t<rhs.t;
- }
- };
- 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;
- }
- 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;
- }
其实很简单,也并非是什么贪心,每个小区间加一个计数器,覆盖一次则计数器加1,最后找最大的计数器即可,代码如下:
题意:如下图形式给多个区间,求最少覆盖多少层,输出结果乘10。
如(1, 3)和(5, 7)则覆盖1层,(1, 5)和(3, 7)由于(3, 5)区间有重叠,所以覆盖了两层。
如(1, 3)和(5, 7)则覆盖1层,(1, 5)和(3, 7)由于(3, 5)区间有重叠,所以覆盖了两层。
下图为示例几种区间覆盖:
public static void main(String[] args) throws NumberFormatException,IOException {Scanner read = new Scanner(System.in);int t = read.nextInt(); //一共有t组测试数据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); //对每组数据都重新初始化s = read.nextInt();m = new int[s][2];for (int j = 0; j < s; j++) {m[j][0] = read.nextInt();m[j][1] = read.nextInt();}//遍历所有桌子,更新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);}}