hdoj1050 Moving Tables(贪心)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1050
题意
有一条走廊,走廊两边各有200个房间,一边的房间编号是奇数,另一边是偶数。现在有n个箱子需要从一个房间移动到另一个房间,移动一个箱子需要10分钟,箱子可以同时移动,但某一段走廊每次只能移动1个箱子,比如(1,3)和(6,8)可以同时移动,而(1,5)和(6,8)不能同时移动,求最少需要多少时间可以把箱子移动完毕。
思路
先将箱子按开始的房间号由小到大排序,然后循环遍历,在遍历的过程中不断去除可以同时移动的箱子(表示箱子已移动完毕),记录循环的次数,直到所有的箱子均已移动完毕。循环次数*10即是答案。由于这是区间相交问题,所以要将所有的房间号映射到一维坐标系中,根据题目中的图可知,对于房间号为k的房间来说(k为偶数),房间k和房间k-1是相对的,也就是偶数k和k-1在坐标轴上对应的是同一位置,所以要将所有编号为偶数k的房间的编号转化成k-1.
代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; struct Node
{
int s, e;
bool visit; //记录箱子是否已经移动完毕,值为true则移动完毕 Node(int s, int e) :s(s), e(e), visit(false) {}
bool operator < (const Node& node)const
{
return s < node.s; //将房间按开始房间号从小到大排序
}
}; int main()
{
//freopen("hdoj1050.txt", "r", stdin);
vector<Node> v;
int m, n;
cin >> m;
while (m--)
{
cin >> n;
v.clear();
int s, e;
for (int i = ; i<n; i++)
{
cin >> s >> e;
if (s > e) //注意输入的开始房间号可能大于结束房间号
swap(s, e);
if (s % == ) //房间号为偶数的,要变成该偶数的前一个奇数
s--;
if (e % == )
e--;
v.push_back(Node(s, e));
} sort(v.begin(), v.end());
int ans = ; //记录循环的次数
int cnt = ; //记录已经移动完毕的箱子的个数
int cur = ; //当前从第cur个箱子开始遍历
int t;
while (cnt != n)
{
for (int i = ; i<n; i++)
{
if (!v[i].visit)
{
cur = i;
t = v[i].e;
v[i].visit = true;
cnt++;
break;
}
}
for (int i = cur+; i<n; i++)
{
if (!v[i].visit && v[i].s > t)
{
cnt++;
t = v[i].e;
v[i].visit = true;
}
}
ans++;
}
cout << ans * << endl;
}
return ;
}
注意点
1、要按开始房间号从小到大排序,而不是结束房间号。手动运行下面的数据有助于理解:
2、输入的开始房间号可能大于结束房间号,此时要将两者交换;
3、对于编号为偶数k的房间,需将其编号转为奇数k-1
测试数据
输入:
输出:
hdoj1050 Moving Tables(贪心)的更多相关文章
- Moving Tables(贪心或Dp POJ1083)
Moving Tables Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28304 Accepted: 9446 De ...
- zstu.2512. Moving Tables(贪心)
Moving Tables Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1182 Solved: 563 Description The famo ...
- HDU1050(Moving Tables:贪心算法)
解题思路: 这种做法是基于hdu2037的做法上考虑的,找出所有可以同时搬运的桌子,然后就很方便求出最短总时间. 还有一种更简单的做法是直接遍历一遍找出与别的重复次数最多的那片区域,重复次数*10就可 ...
- hdu_1050 Moving Tables 贪心
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- POJ 1083 && HDU 1050 Moving Tables (贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdoj 1050 Moving Tables【贪心区间覆盖】
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDOJ 1050 Moving Tables
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- c# dev GridControl多选当前行显示样式问题
由于Dev GridControl在支持多选的时候,如果如果焦点行单独加了样式,Appearance-->FocusedRow & HideSelectionRow 这个时候,鼠标焦 ...
- 《Two Dozen Short Lessons in Haskell》(二十二)递归
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- ETL 自动化测试框架
分享个自己最近在做的自动化测试框架架构图. 数据的测试,入口一般定时任务.可添加参数选择执行任务的范围,也可以选择默认的执行范围.验证测试的为etl测试.数据库中的字段校验(通过对应关系.接口或者SQ ...
- 出了一个js的题。
class test { set xx(v){ console.log('i am set'); this.__ok = v; } get xx(){ console.log('i am get'); ...
- 20155316 2016-2017-2 《Java程序设计》第7周学习总结
教材学习内容总结 1. 时间与日期 1.1 时间的度量 GMT -> UT -> TAI -> UTC 英文 缩写 Greenwich Mean Time GMT Universal ...
- [转]Git忽略提交规则 - .gitignore配置运维总结
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...
- [转]STL 容器一些底层机制
1.vector 容器 vector 的数据安排以及操作方式,与 array 非常相似.两者的唯一区别在于空间的运用的灵活性.array 是静态空间,一旦配置了就不能改变,vector 是动态数组.在 ...
- 防止 Google Smart Lock 记忆错的用户名
默认 chrome 会查找密码上面的那个(非隐藏非禁用)的表单域 如果上面是个短信验证码框,就会将验证码当成用户名提示用户保存. 在用户名 input 上添加 autocomplete="u ...
- linux nginx大量TIME_WAIT的解决办法--转
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 8535 CLOSE_WAIT 5 FIN ...
- HADOOP百度云资料
百度云下载地址: 链接:http://pan.baidu.com/s/1pL56hkv 密码:u4h3 解压密码:www.mukedaba.com