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
- codeforces 264C Choosing Balls (dp)
- CodeForces 264C Choosing Balls dp(水
- Codeforces 264 C. Choosing Balls
- 264C - Choosing Balls DP
- Codeforces Round #162 (Div. 1) C Choosing Balls dp
- 【Coder Force】264C - Choosing Balls (DP高难度思维转换)
- CODEFORCES, 265E Choosing Balls
- Codeforces 67C. Sequence of Balls DP!
- codeforces 67C Sequence of Balls (dp)
- Codeforces 67C Sequence of Balls 编辑距离 dp
- Codeforces 554C Kyoya and Colored Balls 【dp + 组合数学】
- [Codeforces 67C] Sequence of Balls (字符串DP)
- CodeForces 219D Choosing Capital for Treeland (树形dp)
- CodeForces 219D Choosing Capital for Treeland | 树形dp
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
- codeforces 219D Choosing Capital for Treeland(树形DP)
- CodeForces 219D Choosing Capital for Treeland (树形DP)
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- 即使不当码农,你也要知道的15种编程语言
- java 基本类型与封装类的区别
- 循环-04. 验证“哥德巴赫猜想”(20)
- LinearLayout中的layout_weight属性 的使用
- 【Project Euler】1 第一题
- codeforces 264C Choosing Balls (dp)
- 欢迎使用CSDN-markdown编辑器
- poj解题报告——3258
- 【Project Euler】2 第二题
- Kinect深度图与彩色图的坐标校准
- 【Project Euler】3 第三题
- UITextField 限制输入数字及小数点
- 用define定义的 1 和 1ul的区别
- 软件开发项目经理想关河书籍