SGU 259 单机调度问题 +贪心 及此题解法的证明

时间:2022-10-30 21:45:53

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 T i 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 L i 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 T i (1 <= i <= N). Third line contains N integer numbers L i (1 <= i <= N). You can assume that 1 <= T i, L i <= 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[n] 就是最优解。。
 
最优解 :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) 公式推出来
这就类似 归纳推理证明的意味了。。。  
 
注 :  初次写证明如有不正确的  请指正 。。。。。。。。。。。。。。。。
 
附上本人的代码:代码简单就不写注释了。。。。。。。。
 
 
#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[n];
      
	return 0;


}