地鼠游戏

来源:互联网 发布:javascript廖雪峰pdf 编辑:程序博客网 时间:2024/05/02 00:16

http://codevs.cn/problem/1052/
贪心+快排+DP
一开始想复杂了,其实一开始是一起出现,然后比较下停留的时间,从小到大排列,从前面开始DP较大的就加在一起。状态转移方程:f[j]=max(f[j],f[j-1]+a[i].v);也比较容易理解,当前价值等于前一时刻的价值加上当前的价值的最大值,这是前一时刻是因为题目给了一秒的时间间隔,当前时刻最大值是因为同一时刻有多组值。
不过我还有点小疑问。
我输出时间最大值的时候的最大价值,不对,看了
数据是因为两个有两个值。可是 为啥明明100比98大可是98的值却比较大,最后用了一个for循环遍历最大值,才搞定了。
100
11 3 23 49 67 4 29 70 32 12 28 86 73 15 49 41 65 36 2 75 23 45 33 98 19 55 13 31 100 45 95 94 68 54 84 68 26 55 94 80 40 81 9 54 17 38 96 73 56 85 95 90 83 38 32 100 26 44 43 46 4 70 8 30 100 23 98 74 19 19 99 57 66 76 31 10 30 35 57 20 87 59 64 58 16 39 76 26 8 43 91 95 79 1 5 59 50 74 35 79
4 34 78 56 58 100 10 84 87 90 35 17 36 50 85 29 77 46 27 49 88 97 39 77 50 24 58 13 71 47 47 28 92 74 5 18 51 79 25 92 34 3 55 50 32 26 65 62 61 95 64 37 87 89 14 93 69 48 83 60 64 71 91 66 77 43 16 28 9 3 57 26 44 66 48 36 95 9 67 50 99 4 40 78 37 98 11 74 82 1 28 4 76 100 26 2 82 73 11 53
这组数据 用for循环输出DP维护的数组。

#include<iostream>#include<stdio.h>#include<cmath>#include<string.h>#include<map>#include<algorithm>using namespace std;struct mystruct{    int t,v;}a[105];bool cmp(mystruct a,mystruct b){    return a.t<b.t;}int main(){   int f[105]={0};   int n;   cin>>n;   for(int i=0;i<n;i++)        cin>>a[i].t;     for(int i=0;i<n;i++)        cin>>a[i].v;    sort(a,a+n,cmp);   for(int i=0;i<n;i++)   {        for(int j=a[i].t;j>0;j--)            f[j]=max(f[j],f[j-1]+a[i].v);   }   int maxnum=-9999;   for(int i=0;i<100;i++)        maxnum=max(maxnum,f[i]);   cout<<maxnum<<endl;   return 0;}
0 0