【智力大冲浪】
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 214748364711 #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 war15 //.10.2416 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 jl24 {25int t;26int v;27bool operator<(const jl&aa)const28{29 return t<aa.t;30}31 }a[510];32 33 void in(int &x)34 {35int y=1;36char c=g();x=0;37while(c<'0'||c>'9')38{39 if(c=='-')40 y=-1;41 c=g();42}43while(c>='0'&&c<='9')x=x*10+c-'0',c=g();44x*=y;45 }46 void o(int x)47 {48if(x<0)49{50p('-');51x=-x;52}53if(x>9)o(x/10);54p(x%10+'0');55 }56 int main()57 {58freopen("riddle.in","r",stdin);59freopen("riddle.out","w",stdout);60in(m),in(n);61For(i,1,n)62in(a[i].t); 63For(i,1,n)64in(a[i].v),m-=a[i].v;65sort(a+1,a+n+1);66For(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);69Max=-inf;70For(i,1,n)71Max=max(Max,f[i]);72o(Max+m);73return 0;74 }