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

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

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

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

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t, n, vis[405], Max;
struct R {
int start;
int end;
} r[205]; int main() {
scanf("%d", &t);
while (t --) {
Max = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 0; i < n; i ++) {
scanf("%d%d", &r[i].start, &r[i].end);
if (r[i].start > r[i].end)
swap(r[i].start, r[i].end);
for (int j = r[i].start; j <= r[i].end; j ++) {
vis[j] ++;
if (Max < vis[j])
Max = vis[j];
}
if (r[i].start % 2 == 0)//注意由于走廊是对称的,要考虑这种特殊情况
vis[r[i].start - 1] ++;
if (Max < vis[r[i].start - 1])
Max = vis[r[i].start - 1];
if (r[i].end % 2)
vis[r[i].end + 1] ++;
if (Max < vis[r[i].end + 1])
Max = vis[r[i].end + 1];
}
printf("%d\n", Max * 10);
}
return 0;
}

一开始的代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t, n, num, qnum, en;
struct Interval {
int start, end, v;
} q[205]; int cmp (Interval a, Interval b) {
return a.start < b.start;//这里改一下就过了。。
}
int main() {
scanf("%d", &t);
while (t --) {
num = 0; qnum = 0;
memset(q, 0, sizeof(q));
scanf("%d", &n);
for (int i = 0; i < n; i ++) {
scanf("%d%d", &q[i].start, &q[i].end);
if (q[i].start > q[i].end)
swap(q[i].start, q[i].end);
}
sort (q, q + n, cmp);
while (qnum < n) {
for (int i = 0; i < n; i ++) {
if (!q[i].v) {
q[i].v = 1;
qnum ++;
en = q[i].end;
if (en % 2)//由于是对称的,所以要这样讨论。
en ++;
for (int j = 0; j < n; j ++) {
if (q[j].start > en && !q[j].v) {
en = q[j].end;
q[j].v = 1;
qnum ++;
}
}
break;
}
}
num ++;
}
printf("%d\n", num * 10);
}
return 0;
}

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. 我使用过的Linux命令

    我使用过的Linux命令之tee - 重定向输出到多个文件 用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出 ...

  2. Eclipse+EGit的配置注意点, 以及解决Github多个本地仓库之间的冲突

    问题描述 不同本地仓库(e.g. Repo1, Repo2)之间同时修改一个文件时, 出现文件无法merge的情况. 具体表现为, 冲突(红色双向实心箭头)一直存在, 点pull没反应, 点push报 ...

  3. VirtualBox扩展磁盘空间

    进入VB的安装目录, 输入命令 VBoxManage list hdds获得当前所有虚拟机的uuid 选择需要扩展的磁盘, 输入 VBoxManage modifyhd uuid –resize 81 ...

  4. hdu 3980 Paint Chain sg函数

    题目链接 给一个长度为n的环, 两个人轮流涂色, 每次涂m个连续的, 无法继续涂了就输. #include<bits/stdc++.h> using namespace std; #def ...

  5. glib源码安装使用方法

    glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...

  6. Radio Checkbox Select 操作

    一个小总结 <!DOCTYPE html> <html> <head> <meta name="description" content= ...

  7. 夜未央Test1

    积木游戏(block.pas)   [题目描述] 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,最高的积木的最终需要达到h. 在搭建开始之前,没有任何积木(可以看成n ...

  8. PHP下通过file_get_contents\curl的方法实现获取远程网页内容(别忘了还有PhpRPC)

    [php]PHP中file_get_contents()与file_put_contents()函数细节详解 php函数file_get_contents(一) 案例: 早在2010年时候遇到过这样的 ...

  9. ImageMagick 转换 progressive jpeg

    什么是渐进式图片(Progressive JPEG)? 来自 张鑫旭-鑫空间-鑫生活 的解释: 不知诸位有没有注意到,这些jpg格式的图片在呈现的时候,有两种方式,一种是自上而下扫描式的,还有一种就是 ...

  10. spring jar包冲突

    在用Spring+Hibernate做项目时候遇到java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit 网上查得答案 环境 ...