24h購物| | PChome| 登入
2014-01-24 09:40:30| 人氣1,341| 回應0 | 上一篇 | 下一篇

[UVA][dp] 672 - Gangsters

推薦 0 收藏 0 轉貼0 訂閱站台


 Gangsters 

N gangsters are going to a restaurant. The i-th gangster comes at thetime Ti and has the prosperity Pi. The door of the restaurant hasK+1 states of openness expressed by the integers in the range [0, K]. Thestate of openness can change by one in one unit of time; i.e. it either opensby one, closes by one or remains the same. At the initial moment of time thedoor is closed (state 0). The i-th gangster enters the restaurant only if thedoor is opened specially for him, i.e. when the state of openness coincideswith his stoutness Si. If at the moment of time when the gangstercomes 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 is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.

The first line of each dataset contains the values N, K, and T,separated by spaces. ($1 le N le 100, 1 le K le 100, 0 le T le 30000$)

The second line of the dataset contains the moments of time whengangsters come to the restaurant $T_1, T_2, dots, T_N$,separated byspaces. ($0 le T_i le T$for $i = 1, 2, dots, N$)

The third line of the dataset contains the values of the prosperity ofgangsters $P_1, P_2 , dots, P_N$,separated by spaces. ($0 le P_i le 300$for $i = 1, 2, dots, N$)

The forth line of the dataset contains the values of the stoutness ofgangsters $S_1, S_2, dots, S_N$,separated by spaces. ($1 le S_i le K$for$i = 1, 2, dots, N$)


All values in the input file are integers.

Output 

For each dataset, print the single integer - the maximal sum of prosperity of gangsters in the restaurant. In case when nogangster can enter the restaurant the output should be 0.Print a blank line between datasets.

Sample Input 

14 10 2010 16 8 1610 11 15 110 7 1 8

Sample Output 

26



Miguel Revilla
2000-05-22

題目描述:


一家餐館現在陸陸續續有 N 位流氓來訪,這家店的門很特殊,每個時刻可以變大+1不變+0變小-1,

第 0 時刻大小為 0,而每個流氓來時,如果門大小剛好等於他所要求的,他便會進來消費。

反之則否,求最大消費總額。

題目解法:

對時間狀態作 DP,dp[i][j] : 在時間 i 門大小 j 的最大消費總額。

// 一開始想對時間排序,把時間 i 替換成第 i 個人,由於時間可能會重疊,處理較為複雜。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Gangster {
    int t, p, s;
};
int dp[30005][105], w[30005][105];
int main() {
    int testcase;
    int N, K, T;
    int i, j, k;
    Gangster D[105];
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &N, &K, &T);
        for(i = 1; i <= N; i++)    scanf("%d", &D[i].t);
        for(i = 1; i <= N; i++)    scanf("%d", &D[i].p);
        for(i = 1; i <= N; i++)    scanf("%d", &D[i].s);
        memset(w, 0, sizeof(w));
        memset(dp, 0, sizeof(dp));
        for(i = 1; i <= N; i++)
            w[D[i].t][D[i].s] += D[i].p;
        int ret = 0;
        for(i = 0; i < T; i++) {
            for(j = min(i, K); j >= 0; j--) {
                if(j)
                    dp[i+1][j-1] = max(dp[i+1][j-1], dp[i][j] + w[i+1][j-1]);
                dp[i+1][j] = max(dp[i+1][j], dp[i][j] + w[i+1][j]);
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + w[i+1][j+1]);
            }
        }
        for(i = 0; i <= K; i++)       
            ret = max(ret, dp[T][i]);
        printf("%d\n", ret);
        if(testcase)
            puts("");
    }
    return 0;
}

台長: Morris
人氣(1,341) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][搜索] 653 - Gizilch
此分類上一篇:[UVA] 10461 - Difference

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文