博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Moving Tables-贪心
阅读量:7246 次
发布时间:2019-06-29

本文共 3682 字,大约阅读时间需要 12 分钟。

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

 

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
 第一种方法,很明了
对这道题目,核心算法就是贪心了。从最小的房号開始搬运。然后将不冲突的都搬走就能够了
接着就是相邻的两个房间进行特殊处理。假设是5号房间,那么6号房间的走廊就不能用。为什么。请
看题目提供的图。所以接下来就处理过去就能够了,知道没有房间能够搬。
/*Author: 2486Memory: 1416 KB		Time: 0 MSLanguage: G++		Result: AcceptedVJ RunId: 4178448		Real RunId: 14210276*/#include 
#include
#include
using namespace std;const int maxn=200+5;int t,n;struct obje { int s,t; bool operator<(const obje&a)const { return s
objes[i].t)swap(objes[i].s,objes[i].t); vis[i]=false; } sort(objes,objes+n);//按房号開始排序 int cnt=0; bool flag=false; while(true) { int t=0; flag=false; for(int i=0; i
第二种方法就是区间覆盖的方法。属于区间覆盖类型题,对于每一回合操作中记录当前覆盖的最大的值,证明这些覆盖的部分肯定不可以同一时候进行搬运,如此这里还须要一个技巧,就是将5,6或者3,4变为一个数,由于他们拥有相同的走廊。将6当做5处理。和把5当做6处理。他们都是占用同一走廊,所得到的效果是一样的
于是
/*Author: 2486Memory: 1408 KB		Time: 0 MSLanguage: G++		Result: Accepted */#include 
#include
#include
using namespace std;const int maxn=200+5;int t,n,s,tt;int a[maxn];int main() { //freopen("D://imput.txt","r",stdin); scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); scanf("%d",&n); int cnt=0; while(n--){ scanf("%d%d",&s,&tt); if(s>tt)swap(s,tt); s=(s+1)>>1;//将他们5,6缩到一起。区间降低一半 tt=(tt+1)>>1; for(int i=s;i<=tt;i++){ a[i]++; if(a[i]>cnt)cnt=a[i];//求每一回合,区间的最大覆盖值就可以 } } printf("%d\n",cnt*10); } return 0;}

转载于:https://www.cnblogs.com/clnchanpin/p/6770204.html

你可能感兴趣的文章
CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14
查看>>
埋点测试基础篇--什么是站包
查看>>
Linux下搭建SVN服务器及自动更新项目文件到web发布目录(www)
查看>>
63ES6_数据类型_表达式
查看>>
VSFTPD配置虚拟用户 -V2
查看>>
洛谷——P3353 在你窗外闪耀的星星
查看>>
shell 脚本编程
查看>>
数据库优化工程师必看 第一部分(索引、视图)
查看>>
如何谨慎选择企业外部培训师
查看>>
nagios常见错误排查
查看>>
我的友情链接
查看>>
一、Java内存数据库实践之深入浅出Redis - Redis介绍
查看>>
学习小笔记---大话PHP设计模式
查看>>
Linux 用户态与内核态的交互-netlink
查看>>
Gentoo安装(UEFI+Stub_kernel+Systemd+awesome)
查看>>
找出apache日志中访问量最大的IP
查看>>
高可用集群之heartbeat v2--基于CRM实现mysql高可用集群(未完)
查看>>
linux 6.4使用xmanager4的配置
查看>>
Servlet--表单、超链接、转发、重定向4种情况的路径
查看>>
tomcat解压版exe文件启动闪退问题
查看>>