POJ1083 Moving Tables
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35297   Accepted: 11774

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 file. 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 3 + N -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

Source

 
解题思路:
     酒店有400个房间,如下编号对称分布在一条走廊两侧
     1 3 5 ... 397 399
        这里是走廊
     2 4 6 ... 398 400
 
     现在要从房间s->t搬桌子,有N组房间需要搬桌子,每搬一次10分钟(不论两个房间相隔多远).
     对于某组房间,搬桌子期间 s->t 之间的走廊会被占用,
     而其他组房间若没有使用到占用的走廊,则可同时搬,否则要等待.
     特别地,相对的两间房,共用一段走廊, 即若房间3的走廊被占用了,等同于房间4的走廊被占用了.
 
     给定N组需要搬的桌子,求最小可以搬完的时间.
 
 
    解题思路:
     感觉这题有点类似于多线程的同步互斥场景问题.
 
     ① 由于相对的两间房共用同一段走廊,为了方便处理问题,可以把所有奇数房间转换成偶数房间,反之亦可.
        如 1->6 可等价转换成 2->6, 尔后只需要关注一侧的房间即可(共200间)
     ② 根据房间号对走廊进行分割,可分割成200段走廊. 在从房间s->t搬桌子期间,对所使用到的每段走廊计数+1
     ③ 所有桌子搬完后,统计每段走廊的计数值(计数值代表这段走廊总共需要被占用的次数),
        由于题目并不考虑搬动期间的移动过程因素(类比多线程的事务锁),
        因此最大的一个计数值*10分钟 就是所求的最小搬完时间(因为只要走廊被占着,就只能在下一次再搬,不能同时搬)
#include <iostream>
using namespace std; const static int ROOM_NUM = ; // 最大房号
const static int TIME_UNIT = ; // 时间单位 /*
* 把偶数房号转换成奇数房号
* even 偶数房号
* return 奇数房号
*/
int toOdd(int even); void solve(void); int main(void) {
int testCase = ;
cin >> testCase;
for(int t = ; t < testCase; t++) {
solve();
} //system("pause");
return ;
} int toOdd(int even) {
return (even % == ? even - : even);
} void solve(void) {
int useCnt[ROOM_NUM] = { }; // 每个房间前的走廊被使用的次数
int maxUseCnt = ; // 被使用最多的次数 int moveCnt = ; // 需要搬动的桌子组数
cin >> moveCnt;
int* fromRooms = new int[moveCnt]; // 起点房间集
int* toRooms = new int[moveCnt]; // 终点房间集
for(int i = ; i < moveCnt; i++) {
int from, to;
cin >> from >> to; // 使房号小的在前面
fromRooms[i] = (from <= to ? from : to);
toRooms[i] = (from > to ? from : to); // 把房号全部转换成奇数
fromRooms[i] = toOdd(fromRooms[i]);
toRooms[i] = toOdd(toRooms[i]); // 相关房间前占用的走廊被使用次数+1
for(int roomId = fromRooms[i]; roomId <= toRooms[i]; roomId += ) {
useCnt[roomId]++;
if(maxUseCnt < useCnt[roomId]) {
maxUseCnt = useCnt[roomId]; // 登记最大的使用次数
}
}
}
delete[] fromRooms;
delete[] toRooms; // 计算最小的使用时间
maxUseCnt = (maxUseCnt <= || maxUseCnt > moveCnt ? moveCnt : maxUseCnt);
int minUsedTime = maxUseCnt * TIME_UNIT;
cout << minUsedTime << endl;
}

POJ1083 Moving Tables的更多相关文章

  1. POJ1083 Moving Tables(模拟)

    The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...

  2. 解题报告:poj1083 Moving tables

    2017-09-02 19:49:59 writer:pprp 题意说明: 比较简单的题,一开始被吓到了,后来才发现,其实可以用很简单的方法就可以解决: 就是在这样的房间中如果在i 和 j 中之后的1 ...

  3. POJ1083(Moving Tables)--简单模拟

    题目链接:http://poj.org/problem?id=1083 如图所示在一条走廊的两侧各有200个房间,现在给定一些成对的房间相互交换桌子,但是走廊每次只能通过一组搬运, 也就是说如果两个搬 ...

  4. Moving Tables(贪心或Dp POJ1083)

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

  5. zstu.2512. Moving Tables(贪心)

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

  6. HDOJ 1050 Moving Tables

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

  7. 1050 Moving Tables

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

  8. Moving Tables

    Moving Tables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

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

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

随机推荐

  1. [经验分享] MySQL Innodb表导致死锁日志情况分析与归纳【转,纯学习】

    在定时脚本运行过程中,发现当备份表格的sql语句与删除该表部分数据的sql语句同时运行时,mysql会检测出死锁,并打印出日志. 两个sql语句如下: (1)insert into backup_ta ...

  2. android开发解决Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: c.....

    网上常见的方法我都试过,都没能解决,偶然看到的一个方法解决了,在这了记录一下. 在App目录下的build.gradle的android{ ...  ....}中添加如下代码,即可解决.(xx.xx. ...

  3. Gitlab迁移之数据库报错解决

    Gitlab迁移需同版本迁移,恢复过程会出现如下提示: WARNING:  no privileges were granted for "public" 解决方法: 1. 编辑/ ...

  4. Java String类和StringBuffer类的区别

    1.String与StringBuffer的区别简单地说,就是一个变量和常量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象.St ...

  5. python selenium-webdriver 登录验证码的处理(十二)

    很多系统为了防止坏人,会增加各样形式的验证码,做测试最头痛的莫过于验证码的处理,验证码的处理一般分为三种方法 1.开发给我们设置一个万能的验证码: 2.开发将验证码给屏蔽掉: 3.自己识别图片的上的千 ...

  6. tomcat 发布后中文乱码问题

    接口收到数据,使用Eclipse运行调试中文正常显示,发布到Tomcat后中文出现乱码情况: 解决方法: tomcat启动时默认使用系统编码,可更改tomcat bin目录下catalina.bat文 ...

  7. MegaCLi命令总结

    MegaCli命令总结 MegaCli 版本8.00.29,raid卡为lsi 8888elp,固件11.0.1-0036 1    巡读 一MegaCli -adppr -enblauto  -a0 ...

  8. Azure CosmosDB (13) CosmosDB数据建模

    <Windows Azure Platform 系列文章目录> 我们在使用NoSQL的时候,如Azure Cosmos DB,可以非常快速的查询非结构化,或半结构化的数据.我们需要花一些时 ...

  9. 时光如梭,MES生产制造执行系统上线2周年--->2016.08,发个博客展示一下系统的主要功能!

    以下程序是系统当中的主要功能信息,一些相对简单功能就不在此处展示了. 1.模具基础资料Excel导入与模具资料手动更新功能.友情提示:为了避免不必要的麻烦已经将部分信息打码.! 2.配方资料Excel ...

  10. python基础知识13---函数对象、函数嵌套、名称空间与作用域、装饰器

    阅读目录 一 函数对象 二 函数嵌套 三 名称空间与作用域 四 闭包函数 五 装饰器 六 练习题 一 函数对象 1 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 ...