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 ...
随机推荐
- 蓝桥杯 地宫寻宝 DFS 动态规划
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstdl ...
- select 函数小结 -- 转自百度知道
http://zhidao.baidu.com/link?url=UVTXeK4ncKmnwatGUW2deMFylNYBuur-zHmK3w53NXNRpgPbhld2WdkMD766nKl_6Hj ...
- GDB基本用法
基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...
- 谈谈VMware虚拟机中的网络问题
前言:用了好几年的虚拟机,多多少少都会遇到那么一些网络问题,在这里总结一下这么几年在虚拟机中遇到的一些网络问题(主要针对linux)...... 一.VMware相关基础知识 1.bridged(桥接 ...
- JVM性能调优监控工具详解
现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 .... ...
- MySQL练习-主外键多表查询
练习: 1.建立表关系: 请创建如下表,并创建相关约束 USE db1; CREATE TABLE class( cid INT AUTO_INCREMENT PRIMARY KEY, caption ...
- npm_一个有意思的npm包
$ npm install yosay const yosay = require('yosay'); console.log(yosay('Hello, and welcome to my fant ...
- iOS学习笔记(4)— UITableView的重用机制
UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的个数.比如,cell高度为90.那么480 / 90 = 5 ...
- [转]ROS(Robot Operating System)常用环境变量介绍
本文简单介绍ROS系统中常用的环境变量用途及设置方式.ROS系统环境中除了必须配置的环境变量以外,其他的也是十分有用,通过修改变量路径,可以设置ROS系统中log文件存放路径,单元测试结果存放路径等. ...
- 编译安装 zbar 时两次 make 带来的惊喜
为了装 php 的条形码扩展模块 php-zbarcode,先装了一天的 ImageMagick 和 zbar.也许和我装的 Ubuntu 17.10 的有版本兼容问题吧,总之什么毛病都有,apt 不 ...