POJ1083 Moving Tables

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;//安排过了的置为-1
            i++;
            //线性查找
            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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值