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.

  Table moving Reason
Possible ( room 30 to room 50) and (room 60 to room 90) no part of corridor is shared
(room 11 to room 12) and (room 14 to room 13) no part of corridor is shared
Impossible (room 20 to room 40) and (room 31 to room 80) corridor in front of room 31 to room 40 is shared
(room 1 to room 4) and (room 3 to room 6)  corridor in front of room 3 is shared 
(room 2 to room 8) and (room 7 to room 10)  corridor in front of room 7 is shared 

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 

, 1<=
N
<=
200 , that represents the number of tables to move. Each of the following 

lines contains two positive integers 

and 
t, 
representing that a table is to move from room number 

to room number 

(each room number appears at most once in the 

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

  1. 3
  2. 4
  3. 10 20
  4. 30 40
  5. 50 60
  6. 70 80
  7. 2
  8. 1 3
  9. 2 200
  10. 3
  11. 10 100
  12. 20 80
  13. 30 50

Sample Output

  1. 10
  2. 20
  3. 30

题意:要搬东西。每个东西要搬10分钟,几个东西可以同时搬,但是走廊很窄,相同的走廊只能搬一个东西。要求出最快要多久可以搬完。

思路:问题可以转换为,找出要占用同一个走廊的最大值即可,一开始看刘汝佳的最大不相交区间,以为是那样写,写得挺麻烦的结果还是过了。不过有一个不明白的地方,就是改了个排序方式才过的。

代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int t, n, vis[405], Max;
  7. struct R {
  8. int start;
  9. int end;
  10. } r[205];
  11.  
  12. int main() {
  13. scanf("%d", &t);
  14. while (t --) {
  15. Max = 0;
  16. memset(vis, 0, sizeof(vis));
  17. scanf("%d", &n);
  18. for (int i = 0; i < n; i ++) {
  19. scanf("%d%d", &r[i].start, &r[i].end);
  20. if (r[i].start > r[i].end)
  21. swap(r[i].start, r[i].end);
  22. for (int j = r[i].start; j <= r[i].end; j ++) {
  23. vis[j] ++;
  24. if (Max < vis[j])
  25. Max = vis[j];
  26. }
  27. if (r[i].start % 2 == 0)//注意由于走廊是对称的,要考虑这种特殊情况
  28. vis[r[i].start - 1] ++;
  29. if (Max < vis[r[i].start - 1])
  30. Max = vis[r[i].start - 1];
  31. if (r[i].end % 2)
  32. vis[r[i].end + 1] ++;
  33. if (Max < vis[r[i].end + 1])
  34. Max = vis[r[i].end + 1];
  35. }
  36. printf("%d\n", Max * 10);
  37. }
  38. return 0;
  39. }

一开始的代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int t, n, num, qnum, en;
  7. struct Interval {
  8. int start, end, v;
  9. } q[205];
  10.  
  11. int cmp (Interval a, Interval b) {
  12. return a.start < b.start;//这里改一下就过了。。
  13. }
  14. int main() {
  15. scanf("%d", &t);
  16. while (t --) {
  17. num = 0; qnum = 0;
  18. memset(q, 0, sizeof(q));
  19. scanf("%d", &n);
  20. for (int i = 0; i < n; i ++) {
  21. scanf("%d%d", &q[i].start, &q[i].end);
  22. if (q[i].start > q[i].end)
  23. swap(q[i].start, q[i].end);
  24. }
  25. sort (q, q + n, cmp);
  26. while (qnum < n) {
  27. for (int i = 0; i < n; i ++) {
  28. if (!q[i].v) {
  29. q[i].v = 1;
  30. qnum ++;
  31. en = q[i].end;
  32. if (en % 2)//由于是对称的,所以要这样讨论。
  33. en ++;
  34. for (int j = 0; j < n; j ++) {
  35. if (q[j].start > en && !q[j].v) {
  36. en = q[j].end;
  37. q[j].v = 1;
  38. qnum ++;
  39. }
  40. }
  41. break;
  42. }
  43. }
  44. num ++;
  45. }
  46. printf("%d\n", num * 10);
  47. }
  48. return 0;
  49. }

UVAlive 2326 Moving Tables(贪心 + 区间问题)的更多相关文章

  1. uvalive 2326 - Moving Tables(区间覆盖问题)

    题目连接:2326 - Moving Tables 题目大意:在一个走廊上有400个教室, 先在有一些桌子要移动, 每次移动需要十分钟, 但是不同房间的桌子可以在同一个十分钟内移动,只要走廊没有被占用 ...

  2. zstu.2512. Moving Tables(贪心)

     Moving Tables Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1182  Solved: 563 Description The famo ...

  3. Moving Tables(贪心或Dp POJ1083)

    Moving Tables Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28304   Accepted: 9446 De ...

  4. hdu_1050 Moving Tables 贪心

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. HDU1050(Moving Tables:贪心算法)

    解题思路: 这种做法是基于hdu2037的做法上考虑的,找出所有可以同时搬运的桌子,然后就很方便求出最短总时间. 还有一种更简单的做法是直接遍历一遍找出与别的重复次数最多的那片区域,重复次数*10就可 ...

  6. uva live 2326 - Moving Tables

    把房间号映射在一条坐标上,然后排序,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划, ............ 次数*1 ...

  7. --hdu 1050 Moving Tables(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...

  8. hdoj 1050 Moving Tables【贪心区间覆盖】

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. POJ 1083 &amp;&amp; HDU 1050 Moving Tables (贪心)

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. ExpandableListView 箭头样式

    ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项.ExpandableListVive ...

  2. python 程序中设置环境变量

    python 中调用系统命令有三种方法: 1.os.system('command') ,这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 ...

  3. character-RNN模型介绍以及代码解析

    RNN是一个很有意思的模型.早在20年前就有学者发现了它强大的时序记忆能力,另外学术界以证实RNN模型属于Turning-Complete,即理论上可以模拟任何函数.但实际运作上,一开始由于vanis ...

  4. Vim 实用技术,第 1 部分: 实用技巧(转)

    原文链接:http://blog.jobbole.com/20604/ 0. Vim 简介 作为开源世界最重要的编辑器之一(另一个是 Emacs),Vim 以其强大的功能和可定制能力被众多开发者所喜爱 ...

  5. Eclipse连接sql server 2012数据库编程一条龙

    一.java通过jdbc连接sql server 2012 原帖地址:http://blog.csdn.net/stewen_001/article/details/19553173/ 1.sql s ...

  6. tpopela/vips_java

    tpopela/vips_java Implementation of Vision Based Page Segmentation algorithm in Java

  7. 【每周一译】愚蠢的指标:Java中使用最多的关键字

    此翻译纯属个人爱好,由于水平所限,翻译质量可能较低.网络上可能存在其它翻译版本,原文地址:http://blog.jooq.org/2013/08/26/silly-metrics-the-most- ...

  8. 【Oracle】RAC添加新节点

    RAC添加节点: 环境: OS:OEL5.6 RAC:10.2.0.1.0 原有rac1,rac2两个节点.如今要添加rac3节点: 操作过程: 改动三个节点上的/etc/hosts文件 192.16 ...

  9. redis的分布式解决方式--codis

    codis是豌豆荚开源的分布式server.眼下处于稳定阶段. 原文地址:https://github.com/wandoulabs/codis/blob/master/doc/tutorial_zh ...

  10. zoj 2376 Ants

    #include<stdio.h> #include<stdlib.h> ]; int main(void) { int t,n,m,i,len,max,min,mx,mi; ...