【智力大沖浪】
riddle
內存限制: 128M
【題目描述】
例 1 智力大沖浪(riddle.pas)。
【題目描述】
小偉報名參加中央電視臺的智力大沖浪節目。本次挑戰賽吸引了眾多
參賽者,主持人為了表彰大家的勇氣,先獎勵每個參賽者 m 元。先
不要太高興!因為這些錢還不一定都是你的。接下來主持人宣布了比
賽規則:
首先,比賽時間分為 n 個時段(n≤500),它又給出了很多小游戲,每
個小游戲都必須在規定期限 ti 前完成(1≤ti≤n)。如果一個游戲沒能
在規定期限前完成,則要從獎勵費 m 元中扣去一部分錢 wi, wi 為自
然數,不同的游戲扣去的錢是不一樣的。當然,每個游戲本身都很簡
單,保證每個參賽者都能在一個時段內完成,而且都必須從整時段開
始。主持人只是想考考每個參賽者如何安排組織自己做游戲的順序。
作為參賽者,小偉很想贏得冠軍,當然更想贏取最多的錢!
注意:比賽絕對不會讓參賽者賠錢!
【輸入】
輸入文件 riddle.in,共 4 行。
第一行為 m,表示一開始獎勵給每位參賽者的錢;
第二行為 n,表示有 n 個小游戲;
第三行有 n 個數,分別表示游戲 1~n 的規定完成期限;
第四行有 n 個數,分別表示游戲 1~n 不能在規定期限前完成的扣
數。
【輸出】
輸出文件 riddle.out,僅 1 行。表示小偉能贏取最多的錢。
【樣例輸入】
10000
7
4 2 4 3 1 4 6
70 60 50 40 30 20 10
【樣例輸出】
9950
題解:
損失的最少,等價于先都減去再加回來,再跑一遍01背包。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 #include<set> 8 #include<cmath> 9 #include<ctime> 10 #define inf 2147483647 11 #define p(a) putchar(a) 12 #define g() getchar() 13 #define For(i,a,b) for(register int i=a;i<=b;i++) 14 //by war 15 //2017.10.24 16 using namespace std; 17 int m; 18 int n; 19 //int t[510]; 20 //int v[510]; 21 int f[510]; 22 int Max; 23 struct jl 24 { 25 int t; 26 int v; 27 bool operator<(const jl&aa)const 28 { 29 return t<aa.t; 30 } 31 }a[510]; 32 33 void in(int &x) 34 { 35 int y=1; 36 char c=g();x=0; 37 while(c<'0'||c>'9') 38 { 39 if(c=='-') 40 y=-1; 41 c=g(); 42 } 43 while(c>='0'&&c<='9')x=x*10+c-'0',c=g(); 44 x*=y; 45 } 46 void o(int x) 47 { 48 if(x<0) 49 { 50 p('-'); 51 x=-x; 52 } 53 if(x>9)o(x/10); 54 p(x%10+'0'); 55 } 56 int main() 57 { 58 freopen("riddle.in","r",stdin); 59 freopen("riddle.out","w",stdout); 60 in(m),in(n); 61 For(i,1,n) 62 in(a[i].t); 63 For(i,1,n) 64 in(a[i].v),m-=a[i].v; 65 sort(a+1,a+n+1); 66 For(i,1,n) 67 for(register int j=a[i].t;j>=1;j--) 68 f[j]=max(f[j],f[j-1]+a[i].v); 69 Max=-inf; 70 For(i,1,n) 71 Max=max(Max,f[i]); 72 o(Max+m); 73 return 0; 74 }
?