codeforces 264C Choosing Balls (dp)

来源:互联网 发布:双飞燕宏数据下载 编辑:程序博客网 时间:2024/05/29 10:03

这题各种TL,各种wa,且看大犇解析

非常牛逼的将O(n^2)降到O(n)

#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<map>using namespace std;typedef long long ll;const int oo=0x3f3f3f3f;const ll OO=1LL<<61;const int Mod=1000000007;const int maxn=100000+100;ll dp[maxn];int v[maxn];int c[maxn];struct DP{    ll max1,max2;    int maxc1,maxc2;    void Clear()    {        maxc1=maxc2=-1;        max1=max2=-OO;    }    ll get_max(int c)    {        if(c!=maxc1) return max1;        else return max2;    }    void update(int c,ll mx)    {        if(maxc1==-1)        {            max1=mx;            maxc1=c;        }        else if(c==maxc1)        {            if(mx>max1) max1=mx;        }        else if(mx>max1)        {            max2=max1;            maxc2=maxc1;            max1=mx;            maxc1=c;        }        else if(maxc2==-1||mx>max2)        {            max2=mx;            maxc2=c;        }    }}opt;int main(){    int n,q;    scanf("%d %d",&n,&q);    for(int i=0;i<n;i++)        scanf("%d",&v[i]);    for(int i=0;i<n;i++)        scanf("%d",&c[i]),c[i]--;    while(q--)    {        ll temp,ans,a,b;        scanf("%I64d %I64d",&a,&b);        for(int i=0;i<maxn;i++)dp[i]=-OO;        opt.Clear();        dp[c[0]]=b*v[0];        opt.update(c[0],b*v[0]);        for(int i=1;i<n;i++)        {            temp=max(b*v[i],opt.get_max(c[i])+b*v[i]);            temp=max(temp,dp[c[i]]+a*v[i]);            if(temp>dp[c[i]])            {                dp[c[i]]=temp;                opt.update(c[i],temp);            }        }        ans=0;        for(int i=0;i<n;i++)            ans=max(ans,dp[i]);        printf("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击