1083:Moving Tables

题目分析:初看似乎像贪心算法中的活动安排问题,不同的是这里的所有活动(相当于搬桌子的距离)都需要安排。

有四种贪心策略:最短优先,最长优先,最早开始时间优先,最早结束时间优先。活动安排问题采用的是最早结束时间优先。此题要求所有活动都被安排,直观的想同时进行的活动相互之间的间隔最小最好。可以证明最早开始时间优先满足要求。

证明:令S = P1,P2,P3,P4,P5是满足最早开始时间优先的一轮安排。即P1是所有活动中最早开始的,P2是在P1结束之后才开始的所有活动中最早开始的,依次类推。

假设存在最优解A不包含上述S的安排。我们要证明存在一个等价的最优解A‘包含上述的S安排。

由于P1是所有活动中最早开始的,所以A中必存在以P1开始的一轮安排,令为T = P1, T2, T3, T4,...Tn。

同时在A中,令包含 P2的一轮安排为B = B1,B2,...,P2,Bk,Bk+1,.. Bm。

由与P2是与P1相容活动中最早开始的,所以可以交换T2, T3, T4,...Tn与P2,Bk,Bk+1,.. Bm。得到新的安排T‘ = P1,P2,Bk,Bk+1,.. Bm和B' = B1,B2,...,T2, T3, T4,...Tn。

继续寻找包含P3的安排,依次类推,可以得到包含S的一个最优解A'。所以,任何最优解都可以转换为使用最早开始时间优先构造的解。证毕。

先排序O(nlogn),然后每次查找(使用二分查找O(logn))相容的最早开始活动,总复杂度O(nlogn)。由于数量很少,使用选择排序和线性查找即可。

#include <iostream>
using namespace std;
int main(){int n,c;int a,b;cin>>n;while(n>0){n--;cin>>c;int table[200][2];for(int i=0; i<c; i++){cin>>a>>b;a = (a-1)/2;b = (b-1)/2;//小的在前if(a<b){table[i][0] = a;table[i][1] = b;}else{table[i][0] = b;table[i][1] = a;}}//选择排序for(int i=0;i<c-1;i++){int f = i,max = table[i][0];for(int j=i+1;j<c;j++){if(table[j][0] < table[f][0]){max = table[j][0];f = j;}}a = table[i][0];b = table[i][1];table[i][0] = table[f][0];table[i][1] = table[f][1];table[f][0] = a;table[f][1] = b;}int cnt = 0;int i;int newstart;//每次构造一轮最优安排while(true){i=0;//找第一个活动while(i<c && table[i][0]==-1)i++;if(i==c)break;newstart = table[i][1];//不能重合table[i][0]=-1;//安排过了的置为-1i++;//线性查找while(true){while(i<c && (table[i][0]<=newstart || table[i][0]==-1))i++;if(i==c)break;newstart = table[i][1];table[i][0]=-1;i++;}cnt++;}cout<<cnt*10<<endl;}return 0;
}

但是,使用贪心解法相当于构造了一个最有解,题目要求的是最小组数。明显求最优解的复杂度>=求最小组数的复杂度。

显然,最小安排组数>=活动的最大重合数。猜想最小安排组数==活动的最大重合数,下面来证明。令P(m)为最大重合数为m的最小安排组数。

假设P(m) = m。下面用数学归纳法证明。

显然P(1)=1成立,即所有活动都不与其他活动重合。

假设P(k)=k成立;

则对P(k+1),令P(k+1)中有k+1个重合活动的区间为[s1,t1] [s2,t2]...[sn,tn]。则对[s1,t1]中的任意一个活动a1,都可以在[s2,t2].中找到至少一个活动不与a1重合(否则[s2,t2].中共有k+1个活动都与a1重合,则构成一个有k+2个重合活动的区间,矛盾),同时[s1,t1]中去除任意一个活动后,该区间的重合活动数减为k个。由以上两点,可以构造一轮相互不重合的活动安排,使去掉这些活动后,[s1,t1] [s2,t2]...[sn,tn]区间的重合活动数都减为k。所以有P(k+1) = P(k)+1=k+1。命题得证。

复杂度为O(nm)。其中m是整个区间中需要统计的点数。

代码如下:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main(){int n,c;int a,b;cin>>n;while(n>0){n--;cin>>c;int count[200];memset(count,0,sizeof(count));for(int i=0; i<c; i++){cin>>a>>b;if(a>b){int t = a;a = b;b = t;}a=(a-1)/2;b=(b-1)/2;for(;a<=b;a++){count[a]++;}}int max = 0;for(int j=0;j<200;j++){if(count[j]>max)max=count[j];}cout<<max*10<<endl;}return 0;
}

题目:
时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
The output should contain the minimum time in minutes to complete the moving, one per line.
样例输入
3
4
10 20
30 40
50 60
70 80
2
1 3
2 200
3
10 100
20 80
30 50
样例输出
10
20
30

转载于:https://www.cnblogs.com/zhuyuanhao/archive/2012/10/03/3262880.html

POJ1083 Moving Tables相关推荐

  1. POJ-1083 Moving Tables(imos算法)

    这一题有两个陷阱: 给出的起始点可能小于终点 ,所以必须要加个判断,让第二个数大于第一个数 区间算的时候,一定是奇数到偶数,才能做到真正的区间全覆盖.所以如果第二个数是奇数,要加一变为偶数:第一个数为 ...

  2. hdu 1050 Moving Tables

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

  3. Moving Tables(贪心)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 原题: Moving Tables Time Limit: 2000/1000 MS (Java/O ...

  4. 【POJ1083】 Moving Tables (并行的搬运)

    BUPT2017 wintertraining(15) #6E 题意 房间1和2,3和4,...,399和400共用一节走廊,有q次从房间li到ri的搬运桌子,一次搬运10分钟.两个搬运如果走廊有重叠 ...

  5. uva live 2326 - Moving Tables

    把房间号映射在一条坐标上,然后排序,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划, ............ 次数*1 ...

  6. hdu 1050 Moving Tables

    http://acm.hdu.edu.cn/showproblem.php?pid=1050 对与每个房间前面过道,操作一次标记一次,标记的最多的次数就是必须用的时间. 1 #include < ...

  7. poj 1083 Moving Tables

    题目 两种做法,开始用贪心做的,有种情况没考虑到,结果排序错了. 这个例子,感觉上有三个交点,以为是30,其实是20. 贪心代码: #include <iostream> #include ...

  8. HDU 1050 NYOJ 220 Moving Tables

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050  http://acm.nyist.net/JudgeOnline/problem.php?pi ...

  9. ICPC程序设计题解书籍系列之九:罗勇军《算法竞赛入门到进阶》

    罗书<算法竞赛入门到进阶>题目一览 第1章 算法竞赛概述 HDU1000 HDU1089-HDU1096 A+B for Input-Output Practice (I)-(VIII)( ...

最新文章

  1. 如何利用python的newspaper包快速爬取网页数据
  2. 2018-4-15摘录笔记,《网络表征学习前沿与实践》 崔鹏以及《网络表征学习中的基本问题初探》 王啸 崔鹏 朱文武
  3. javascript经典实例_提升JavaScript变量的方法有哪些?
  4. 从壹开始前后端分离【 .NET Core2.2 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储+服务+抽象接口模式...
  5. Rails 定时任务——whenever实现周期性任务
  6. list根据对象进行排序
  7. ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
  8. 电路板必须用c语言编程吗,上大学才知道绿油油的板子叫PCB,如何成为一名电子工程师...
  9. jquery ajax请求 清除缓存
  10. spark partition
  11. 《剑指offer》第一题(重载赋值运算符)
  12. 电机编码器调零步骤_伺服电机编码器调零对位方法
  13. CF EDU - E. Lomsat gelral 树上启发式合并
  14. 面试者应向公司问什么问题?
  15. 树莓派的产品定位,以及探讨与ARM开发板的区别
  16. 杨老师课堂_Java教程第一篇之认识计算机
  17. 如何设置桌面图标大小
  18. 摄影构图学83年绝版_点评2张粉丝投稿,有1处共同错误,3个摄影技巧“变废为宝”...
  19. 独立显卡安装ubuntu
  20. 动态规划解决最长公共子序列

热门文章

  1. DNS 与 活动目录 的关系
  2. 终于知道如何才能取得HtmlEditor里的数据了
  3. 概率编程库Pymc3案例之Coal mining disasters
  4. 2011年最后一篇博客—就医感慨!
  5. 读张鸣-《辛亥:摇晃的中国》感
  6. vc操作windows服务(services.msc)
  7. Pandas简明教程:五、Pandas简单统计操作及通用方式
  8. opencv轮廓及点在轮廓内判断
  9. 自动设置图片的序号_word表格技巧:如何在表格中填充序号并能自动更新
  10. 线程池的几个重要参数?—— 七大参数