Problem Description

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.

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

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

[背包问题]
有一个背包，背包容量是M=150。有7个物品，物品不可以分割成任意大小。要求尽可能让装入背包中的物品总价值最大，但不能超过总容量。

物品 A   B   C   D  E   F  G
重量 35 30 60 50 40 10 25
价值 10 40 30 50 35 40 30

目标函数： ∑pi最大
约束条件是装入的物品总重量不超过背包容量：∑wi<=M( M=150)

（1）根据贪心的策略，每次挑选价值最大的物品装入背包，得到的结果是否最优？
（2）每次挑选所占重量最小的物品装入是否能得到最优解？
（3）每次选取单位重量价值最大的物品，成为解本题的策略。

（1）贪心策略：选取价值最大者。

W=30
物品： A  B   C
重量：28 12 12
价值：30 20 20

（2）贪心策略：选取重量最小。它的反例与第一种策略的反例差不多。

（3）贪心策略：选取单位重量价值最大的物品。

W=30
物品： A  B   C
重量：28 20 10
价值：28 20 10

【注意：如果物品可以分割为任意大小，那么策略3可得最优解】
对于选取单位重量价值最大的物品这个策略，可以再加一条优化的规则：对于单位重量价值一样的，则优先选择重量小的！这样，上面的反例就解决了。
但是，如果题目是如下所示，这个策略就也不行了。
W=40
物品：A B C
重量：25 20 15
价值：25 20 15
附：本题是个NP问题，用贪心法并不一定可以求得最优解，以后了解了动态规划算法后本题就有了新的解法。

``` #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define SIZE 404

using namespace std;

typedef struct item{
int begin, end;
}item;
item room[SIZE];
bool visit[SIZE];
int n;

bool cmp(const item& a, const item& b)
{
return a.begin < b.begin;
}

void Traverse(int cnt, int sum)
{
if(sum == n)
{
printf();
return;
}
, count = ;
; i<n; ++i)
{
if(!visit[i] && temp < room[i].begin)
{
count++;
temp = room[i].end;
visit[i] = true;
}
}
Traverse(cnt+, sum+count);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int T, left, right;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
; i<n; ++i)
{
scanf("%d", &left);
room[i].begin = left = left + (left % );
scanf("%d", &right);
room[i].end = right = right + (right % );
if(left > right)
{
room[i].begin = right;
room[i].end = left;
}
}
memset(visit, false, sizeof(visit));
sort(room, room+n, cmp);
Traverse(, );
}
}```

## HDU ACM 1050 Moving Tables的更多相关文章

1. HDOJ 1050 Moving Tables

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

2. 1050 Moving Tables

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

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

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

4. hdu 1050 Moving Tables 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...

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

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

6. hdu 1050 Moving Tables

http://acm.hdu.edu.cn/showproblem.php?pid=1050 这个题我首先直接用的常规贪心,用的和那个尽可能看更多完整节目那种思路.但是.......一直WA....T ...

7. 【HDOJ】1050 Moving Tables

贪心问题,其实我觉得贪心就是合理的考虑最优情况,证明贪心可行即可.这题目没话多久一次ac.这道题需要注意房间号的奇偶性.1 3.2 4的测试数据.答案应该为20. #include <stdio ...

8. zstu.2512. Moving Tables(贪心）

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

9. Moving Tables(贪心或Dp POJ1083)

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

## 随机推荐

1. 源程序版本管理软件和项目管理软件，Github注册流程

目前流行的源程序版本管理软件和项目管理软件:Microsoft TFS,Github,SVN,Coding 各自的优缺点: Microsoft TFS: 优点: tfs核心的,是对敏捷,msf,cmm ...

2. 使用PopupWindow

3. C#中ref和out的使用与区别

C#中ref关键字和out关键字所实现的功能差不多,都是指定一个形参按照引用传递而不是实参的副本传递.但是二者适用场景还是有些区别的:out适合用在需要retrun多个返回值的地方,而ref则适合用在 ...

4. 重回程序员之路。重写博客。我的ecshop小京东二开问题汇总与解决方案。

5. About Inside the Azure Storage Outage of November 18th

Channel 9的官方解释 http://channel9.msdn.com/posts/Inside-the-Azure-Storage-Outage-of-November-18th 曾经在自己 ...

6. iOS 非ARC基本内存管理系列总结6 -设计微博模型

设计简单的微博模型:用User类和Status类来模拟实现 在非ARC机制下有两种方式,两者没有太大的区别之所以写了两种只是为了方便学习和对比两种写法! 第一种:没有使用atuorelease和自动释 ...

7. 使用sql生成UUID

在SQLServer中使用该sql语句可以生成GUID:select cast(NEWID() as varchar(36)) as uuid 通过一下语句将GUID中的'-'字符去掉: select ...

8. 一款兼容pc 移动端的tab切换

利用touchslider.js插件来制作的tab切换,可任意修改很方便~~~ 样式: <style> .box-163css{ width:100%; position:relative ...

9. Unity3D的几种坐标系

原地址:http://www.cnblogs.com/martianzone/p/3371789.html http://www.cnblogs.com/88999660/archive/2013/0 ...

10. Linux协议栈函数调用流程

普通网络驱动程序中必须要调用的函数是eth_type_trans(略),然后向上递交sk_buff时调用netif_rx()(net/core/dev.c).其函数中主要几行 __skb_queue_ ...