POJ 1036 -- Gangsters

Gangsters
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12906   Accepted: 3675

 

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.

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.

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

Source

 
思路:从题目叙述中可以得到一个限制,即如果(当前时刻-上一个可以进入餐厅的时刻)< abs(si-sj),那么在当前这种情况下,当前这个人就进不了餐厅。因为每个时刻k最多+1.
    另外还有一个细节,就是判断当前的限制时要确定前面的j在这种情况下能取的到。
    最后要注意的是,不是最后一个就是答案,要再判断一遍所有情况的最大值。
 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 using namespace std;
 8 int f[30005];
 9 struct data  
10 {
11     int t,p,s;
12     bool operator <(const data &a)const //按时间先后排序 
13     {
14         if(a.t>t) return true;
15         return false;
16     }
17 }a[101];
18 
19 int main()
20 {
21     int N,K,T;
22     scanf("%d%d%d",&N,&K,&T);
23     for(int i=1;i<=N;i++) scanf("%d",&a[i].t);
24     for(int i=1;i<=N;i++) scanf("%d",&a[i].p);
25     for(int i=1;i<=N;i++) scanf("%d",&a[i].s);
26     sort(a+1,a+N+1);
27     memset(f,-1,sizeof(f));
28     f[0]=0;
29     for(int i=1;i<=N;i++)
30         for(int j=0;j<i;j++)
31             if((a[i].t-a[j].t) >= abs(a[i].s-a[j].s) && f[j]!=-1)  //记得加abs,因为排序是按t排的,s不一定靠前的就小 
32                 f[i]=max(f[i],f[j]+a[i].p);
33     int ans=0;
34     for(int i=1;i<=N;i++)
35         if(f[i]>ans) ans=f[i];
36     printf("%d",ans);
37     //system("pause");
38     return 0;
39 }
POJ1036
原文地址:https://www.cnblogs.com/YXY-1211/p/7144632.html