SGU 259 单机调度问题 +贪心 及此题解法的证明
2014-08-02 17:50
441 查看
转自:http://blog.csdn.net/oceanlight/article/details/7862104
259. Printed PR
time limit per test: 0.5 sec.memory limit per test: 65536 KB
input: standard
output: standard
Small PR-agency got an order to print a very big lot of agitational materials. Agency should print and deliver N leaflets. Agency is very small and has only one printer, but it can
print any leaflet. Leaflets can be different, so it is possible that times of printing of leaflets will differ. To print i-th leaflet the printer needs Ti minutes. When leaflet is printed, it should be immediately delivered to its addressee. The
agency has unlimited number of couriers, so the courier gets the leaflet as soon as it printed and goes to the addressee. It takes Li minutes to deliver i-th leaflet. You were hired by agency to calculate the minimal time required to finish the
job (this is an interval of time from the beginning of printing to the moment when the last leaflet is delivered), considering that the leaflets can be printed in any order.
Input
The first line contains a number of leaflets - integer number N (1 <= N <= 100). The second line contains N integer numbers Ti (1 <= i <= N). Third line contains N integer
numbers Li (1 <= i <= N). You can assume that 1 <= Ti, Li <= 1000.
Output
You should output only one number - the answer for the problem.
Sample test(s)
Input
2 2 1 2 1
Output
4
单机调度问题是 : 假设 有一台机器以及在这个机器上完成的N个作业a1,a2 ,a3........an,已知处理作业aj所需的时间为tj,作业aj收益为pj,作业aj完成的最后期限为dj。机器在一个时刻只能处理一个作业。如果某作业aj在最后期限dj之前完成,则获得收益pj,若在最后期限之前没有完成,则没有收益。
声明 此题是个水题。。
这个题的题意是 有一台打印机 要打印N种leaflet(传单),并要送到接收人那里 。 而送leaflet 的人是无上限的 但只有一个打印机。。。。。
已知 打印每种leaflet 的时间 Ti , 送达接受人哪里 要 Li , 求从开始打印到送完的最短时间。
解法 : 首先按照递送的时间 Li 排序 ,然后把所有的leaflet 遍历一遍 ,贪心就可求出最优解。
sum = T1+T2+...+Ti。前i个leaflet 的打印所需时间Ti的综合
num[1]=T1+L1;
num[i]=max(num[i-1],sum+Li)
用次公式递推出 num
就是最优解。。
最优解 :Time = T1+T2+T3+......+Tn + max( Li-(Ti+1 +...+Tn) )
我们只要让 max( Li-(Ti + Ti+1 +...+Tn) ) 最小就可。。。。
特殊情况 所有的T都相同的时候 ,很容易得出 。。。按Li 从大到小排序。。。。
证明 : (对于仅有两个对象a,b时)若Lb <La 已知Tb ,Lb ,Ta,Tb 。则 Lb - Ta<La 于是La>max(Lb,La-Tb)。其中La为b在a前边max( Li-(Ti+1 +...+Tn) )的情况。 max(L(b,La-Tb)为a在b前边max( Li-(Ti+1 +...+Tn) )的情况。 因此a排在b的前边更优
则 当Lb <La 时 a 应排在 b的前边
对于多个对象是 设 c d 为序列中的两个 设c排在d前 则Lc <Ld。 c在d 前时为 max(Lc -Td -(Td+1 +......+Tn),Ld-(Td+1+........TN)) , c,d互换时 为max(Ld-Tc-(Td+1+....+Tn),Lc-(Td+1+......Tn))
因为max(Lc-Td,Ld) >max(Ld-Tc,Lc) 所以 c ,d 互换即d排在c之前值是使解为最优解 。所以要排序
所以 L 大的应该排在 L小的前边。。。
前边只是给出为什么要排序。。。 一般为什么排序的原因是 换顺序 特殊情况反正即可
但是为什么排序后就是最优解 。。。 排序后就能够 根据
num[1]=T1+L1;
num[i]=max(num[i-1],sum+Li) 公式推出来
这就类似 归纳推理证明的意味了。。。
注 : 初次写证明如有不正确的 请指正 。。。。。。。。。。。。。。。。
附上本人的代码:代码简单就不写注释了。。。。。。。。
[cpp] view
plaincopy
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n ;
struct leaflet{
int T;
int L;
bool operator <(const leaflet b)const{
if(L>b.L)
return true;
return false ;
}
}lt[120];
int t[120];
int l[120];
int dp[120];
int max(int a,int b){
return (a>b)?a:b;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&t[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&l[i]);
}
for(int i=1;i<=n;i++){
leaflet a = {t[i],l[i]};
lt[i]=a;
}
sort(lt+1,lt+n+1);
dp[1]=lt[1].T+lt[1].L;
int sum = lt[1].T;
for(int i=2;i<=n;i++){
sum += lt[i].T;
dp[i]=max(dp[i-1],sum+lt[i].L);
}
cout << dp
;
return 0;
}
相关文章推荐
- SGU 259 单机调度问题 +贪心 及此题解法的证明
- SGU 259 Printed PR 贪心 单机调度问题
- 多机调度问题的贪心解法
- 《编程之美》读书笔记(四):卖书折扣问题的贪心解法
- 《编程之美——微软技术面试心得》的C#实现---电梯调度问题解法一
- 对于期末考试 "我" (这是个名字)的贪心问题的证明_题型类似于zoj1877
- HDU 3466 Proud Merchants 01背包 单机调度问题
- 《编程之美》读书笔记(四): 卖书折扣问题的贪心解法
- poj 2751 双机调度问题Johnson算法(贪心)
- hdu 4398 Template Library Management (贪心 最优调度问题)
- 算法导论9-2 邮局位置问题的带权中位数解法证明
- 最优调度(贪心)问题
- 单机调度问题
- 贪心算法之多机调度问题
- 编程之美---小飞的电梯调度问题 (停k层的解法) (正在怀疑该解法的正确性)
- 多机调度问题(贪心)
- 《编程之美》读书笔记(四):买书折扣问题的贪心解法
- 教室课程调度问题的两种解法(区间着色问题)
- 贪心-区间调度问题
- 用贪心算法近似求解 Loading Balance 问题(作业调度的负载均衡)