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 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.


The output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

10 20
30 40
50 60
70 80
1 3
2 200
10 100
20 80
30 50

Sample Output




 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 struct time
 6 {
 7     int start;
 8     int over;
 9 } a[210];
10 int cmp(time c,time b)
11 {
12     return c.start<b.start;
13 }
14 int vis[210];
15 int main()
16 {
17     int t;
18     scanf("%d",&t);
19     while(t--)
20     {
21         memset(vis,0,sizeof(vis));
22         int n;
23         scanf("%d",&n);
24         for(int i=0; i<n; i++)
25             scanf("%d%d",&a[i].start,&a[i].over);
26         for(int i=0;i<n;i++)
27         {
28             if(a[i].start>a[i].over)
29             {
30                 int y=a[i].start;
31                 a[i].start=a[i].over;
32                 a[i].over=y;
33             }
34             if(a[i].start%2==1)
35             a[i].start=a[i].start/2+1;
36             else a[i].start/=2;
37             if(a[i].over%2==1)
38                 a[i].over=a[i].over/2+1;
39             else
40             a[i].over/=2;
41         }
42         sort(a,a+n,cmp);
43         int step=0;
44         int temp;
45         for(int i=0; i<n; i++)
46         {
48             temp=a[i].over;
49             if(vis[i]==0)
50             {
51                 step++;
52                 for(int j=0; j<n; j++)
53                 {
54                     if(a[j].start>temp&&vis[j]==0)
55                     {
56                         temp=a[j].over;
57                         vis[j]=1;
58                     }
59                 }
60             }
61         }
62         printf("%d\n",step*10);
63     }
64 }
时间: 02-25


LightOJ 1166 Old Sorting 置换群 或 贪心 水题

LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : 2017-07-20 14:45:30 * @FileName: LightOJ 1166 贪心 或 置换群 水题.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.co

hdu 5835 Danganronpa 贪心+水题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5835 [题意]有n种礼物,每个有ai个,现在开始给每个人发礼物,每人一个普通礼物和神秘礼物,相邻两人的普通礼物必须不同,每个礼物都可以作为神秘礼物/普通礼物,问最多可以发给多少人. [解题思路] 答案肯定是小于等于sum/2,因为每个小朋友得有2礼物.然而数据太水,sum/2也过了. 我们先考虑平凡的礼物,因为平凡的礼物相邻桌子上不能相同,故先把数量最多的礼物相邻的交替用作平凡的礼物. 如果n=3,

HDU 4334——Trouble——————【贪心&amp;水题】

Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5388    Accepted Submission(s): 1494 Problem Description Hassan is in trouble. His mathematics teacher has given him a very difficult pro

Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)

Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles. The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people


 题目:有n个上午的任务和下午的任务,分配给司机,如果工作总时间超过d,超过的部分要给加班费: 现在让你安排任务,问最小的加班分花费. 思路:将下午的工作时间a按降序排序,晚上时间b按升序排序,不难发现只有将这两组按下标一一配对才能是总花费最小(将任意b[i],b[j]调换位置都不如原来的方案更优) #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include&

poj Yogurt factory (贪心水题)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int main() { int i,n,s; __int64 sum ; int cost[10010],num[10010]; while(~scanf("%d%d",&n,&s)) { for(i=0;i<n;i++) scanf("%d%d&qu

装箱问题 (贪心水题)

[题目描述] 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6.这些产品通常使用一个 6*6*h 的长方体包裹包装然后邮寄给客户.因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量. [题目链接] http://noi.openjudge.cn/ch0406/19/ [算法] 由于需要装箱的产品总面积一定,则最优情况是箱子浪费面积最少.6,5,4,3所需包裹数是一定的,然后尽可能

HDU1009_FatMouse&amp;#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU1009_FatMouse&#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g