Comb CodeForces - 46E (动态规划)
题面
Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table n × m panels in size with integers written on the panels.
题意
在一个\(n * m\)的矩阵中,第\(i\)取前\(c_i\)个元素,要求\(c1 > c2 < c3 > c4 < ...\) 求最后取到的元素和的最大值.
思路
\(dp[i][j]\) 表示第\(i\)行取\(j\)个元素的最大值.
在奇数行和偶数行按不同的顺序更新就行了.
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 1689;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
ll dp[maxn][maxn];
ll num[maxn][maxn];
ll sum[maxn];
int main() {
ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%lld",&num[i][j]);
}
}
ll ans = -Inf;
for(int i=1;i<=n;i++){
if(i&1){
ll mx = -Inf;
for(int j=1;j<=m;j++){
sum[j]=sum[j-1]+num[i][j];
dp[i][j]=mx+sum[j];
mx = max(mx,dp[i-1][j]);
}
}else{
ll mx=-Inf;
for(int j=1;j<=m;j++){
sum[j]=sum[j-1]+num[i][j];
}
for(int j=m;j>=1;j--){
dp[i][j]=mx+sum[j];
mx = max(mx,dp[i-1][j]);
}
}
}
for(int i=n;i<=n;i++){
for(int j=1;j<=m;j++){
ans=max(ans,dp[i][j]);
// cout<<dp[i][j]<< " ";
}
// cout<<endl;
}
printf("%lld\n",ans);
return 0;
}
如需转载,请注明出处
如有侵权,联系删除
2290713181@qq.com
如有侵权,联系删除
2290713181@qq.com