icpc2018焦作Mathematical Curse(动态规划)
Posted windfreedom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了icpc2018焦作Mathematical Curse(动态规划)相关的知识,希望对你有一定的参考价值。
ac代码:
#include<bits/stdc++.h> #define per(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; //#define int long long const ll inf =2333333333333333LL; const double eps=1e-8; int read(){ char ch=getchar(); int res=0,f=0; while(ch<‘0‘ || ch>‘9‘){f=(ch==‘-‘?-1:1);ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){res=res*10+(ch-‘0‘);ch=getchar();} return res*f; } // ------------------------head #define mod 1000000009 const int siz=10005; ll dp[1005][6],k; int T,n,m,a[1005]; char ch[6]; ll fun(int rhs,ll d,char op){ if(op==‘+‘)return d+(ll)rhs; else if(op==‘-‘)return d-(ll)rhs; else if(op==‘*‘)return d*(ll)rhs; else if(op==‘/‘)return d/(ll)rhs; } signed main() { scanf("%d",&T); while(T--){ scanf("%d%d%lld",&n,&m,&k); ll ans=-inf; per(i,1,n)scanf("%d",&a[i]); per(i,1,m)cin>>ch[i]; ll _mi[1005][6]; per(i,0,n)dp[i][0]=_mi[i][0]=k; per(i,1,m){per(j,0,n)_mi[j][i]=inf;per(j,0,n)dp[j][i]=-inf;} per(i,1,n){ dp[i][1]=max(dp[i-1][1],fun(a[i],k,ch[1])); _mi[i][1]=min(_mi[i-1][1],fun(a[i],k,ch[1])); } per(i,2,m){//区分正数和负数 per(j,i,n){ if(a[j]<0&&(ch[i]==‘*‘||ch[i]==‘/‘)){ dp[j][i]=max(dp[j-1][i],fun(a[j],_mi[j-1][i-1],ch[i])); _mi[j][i]=min(_mi[j-1][i],fun(a[j],dp[j-1][i-1],ch[i])); } else { ll ma=dp[j-1][i-1]; dp[j][i]=max(dp[j-1][i],fun(a[j],dp[j-1][i-1],ch[i])); _mi[j][i]=min(_mi[j-1][i],fun(a[j],_mi[j-1][i-1],ch[i])); } } } if(m==1){ ll res=-inf; per(i,1,n)res=max(res,dp[i][1]); printf("%lld ",res); } else printf("%lld ",dp[n][m]); } return 0; }
以上是关于icpc2018焦作Mathematical Curse(动态规划)的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2018 焦作赛区网络预赛 B. Mathematical Curse << DP
ACM-ICPC 2018 焦作赛区网络预赛 B. Mathematical Curse << DP