POJ 1036 Gangsters 水dp。。

来源:互联网 发布:安徽农业大数据平台 编辑:程序博客网 时间:2024/05/16 05:51

分析:假设大家都知道题意的基础上, - -,题目的数据中 s 是必然在[0,K]之间的, t是在[0,T]之间的,所以每个点都是合法的,然后我们按照时间排序,然后以从当前点到结束能获得的最大值为状态进行转移,转移方程是d[i] = max(d[j] + person[i].p) (person[j].t-person[i].t >= abs(person[j].s - person[i].s),值得一提的是,我的方法是在最前面加了一个0点,或者可以最后遍历一遍找到最大值,前提是一样满足状态转移方程。最后,如果像我一样加0点的话,记得不要把0点参与排序,排序如果不稳定,会被其他时间为0的顶到后面,就错了。

代码:

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>using namespace std;struct Person{    int t, p, s;    bool operator<(const Person &a) const{        return t < a.t;    }}person[105];int d[105];int main(){    int N, K, T;    while(scanf("%d%d%d", &N, &K, &T) == 3){        memset(d, 0, sizeof(d));        for(int i = 1; i <= N; i++) scanf("%d", &person[i].t);        for(int i = 1; i <= N; i++) scanf("%d", &person[i].p);        for(int i = 1; i <= N; i++) scanf("%d", &person[i].s);        sort(person + 1, person + N + 1);        for(int i = N; i >= 0; i--){            int ma = 0;            for(int j = i + 1; j < N + 1; j++){                if(abs(person[j].t - person[i].t) >= abs(person[j].s - person[i].s)){                    ma = max(ma, d[j]);                }            }            d[i] = ma + person[i].p;        }        printf("%d\n", d[0]);    }    return 0;}


0 0