题意:宾馆有个可以伸缩的门,每秒钟可以伸长1个单位,或者缩小1个单位,或者原地不动。有N个强盗,每个强盗会在t的时间内到达并按,如果此时门的开方度和他的身材s正好相等,这个强盗就会进来,然后你就能得到p的加分。
初始状态门是关闭的,让你求出在t时刻得到的最大得分。
分析:按时间从小到大排序,t时间内最大的得分由t之前的时刻决定,满足无后效性,每一个时刻都能得到最优解,满足最有子结构,所以DP。
F[i]表示第i个人。
当身材之差小于时间之差时F[i]=max(f[i],f[j]+ple[i].p) 0<=j<i 但得满足一个条件,第j个人已经进去,否则门伸长的宽度可能会小于身材。
Description
N gangsters are going to a restaurant. The i-th gangster comes at the time Ti and has the prosperity Pi. The door of the restaurant has K+1 states of openness expressed by the integers in the range [0, K]. The state of openness can change by one in one unit of time; i.e. it either opens by one, closes by one or remains the same. At the initial moment of time the door is closed (state 0). The i-th gangster enters the restaurant only if the door is opened specially for him, i.e. when the state of openness coincides with his stoutness Si. If at the moment of time when the gangster comes to the restaurant the state of openness is not equal to his stoutness, then the gangster goes away and never returns.
The restaurant works in the interval of time [0, T].
The goal is to gather the gangsters with the maximal total prosperity in the restaurant by opening and closing the door appropriately.
The restaurant works in the interval of time [0, T].
The goal is to gather the gangsters with the maximal total prosperity in the restaurant by opening and closing the door appropriately.
Input
?The first line of the input file contains the values N, K, and T, separated by spaces. (1 <= N <= 100 ,1 <= K <= 100 ,0 <= T <= 30000 )
?The second line of the input file contains the moments of time when gangsters come to the restaurant T1, T2, ..., TN, separated by spaces. ( 0 <= Ti <= T for i = 1, 2, ..., N)
?The third line of the input file contains the values of the prosperity of gangsters P1, P2, ..., PN, separated by spaces. ( 0 <= Pi <= 300 for i = 1, 2, ..., N)
?The forth line of the input file contains the values of the stoutness of gangsters S1, S2, ..., SN, separated by spaces. ( 1 <= Si <= K for i = 1, 2, ..., N)
All values in the input file are integers.
?The second line of the input file contains the moments of time when gangsters come to the restaurant T1, T2, ..., TN, separated by spaces. ( 0 <= Ti <= T for i = 1, 2, ..., N)
?The third line of the input file contains the values of the prosperity of gangsters P1, P2, ..., PN, separated by spaces. ( 0 <= Pi <= 300 for i = 1, 2, ..., N)
?The forth line of the input file contains the values of the stoutness of gangsters S1, S2, ..., SN, separated by spaces. ( 1 <= Si <= K for i = 1, 2, ..., N)
All values in the input file are integers.
Output
Print to the output file the single integer ?the maximal sum of prosperity of gangsters in the restaurant. In case when no gangster can enter the restaurant the output should be 0.
Sample Input
4 10 20 10 16 8 16 10 11 15 1 10 7 1 8
Sample Output
26
#include <iostream> #include <algorithm> #include <cmath> #define N 30000 using namespace std; struct pep { int t,p,s; }gest[N]; bool cmp(pep x,pep y) { return x.t<y.t; } int dp[1000]; int main() { int n,k,t; while(cin>>n>>k>>t) { for(int i=1;i<=n;i++) cin>>gest[i].t; for(int i=1;i<=n;i++) cin>>gest[i].p; for(int i=1;i<=n;i++) cin>>gest[i].s; gest[0].t=0; gest[0].p=0; gest[0].s=0; sort(gest+1,gest+1+n,cmp); int ans=0; dp[0]=0; for(int i=1;i<=n;i++) { for(int j=i-1;j>=0;j--) if(dp[j]>=gest[j].p)//下一步做差的基础是第j个人已经进去。 if(gest[i].t-gest[j].t>=abs(gest[i].s-gest[j].s)) dp[i]=max(dp[i],dp[j]+gest[i].p); ans=max(ans,dp[i]); } cout<<ans<<endl; } return 0; }