码迷,mamicode.com
首页 > 其他好文 > 详细

Two Melodies CodeForces - 813D

时间:2019-01-04 00:20:15      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:for   转移   size   ble   pre   problem   class   http   mes   

https://codeforces.com/problemset/problem/813/D  

DP好难啊.......

 

dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值

关键要保证转移时两条链不能相交

 

 

#include <iostream>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
const int N = 5e3+10, M = 1e7+10;
int n;
int a[N], dp[N][N], x[M], y[M];

int main() {
    cin>>n;
    REP(i,1,n) cin>>a[i];
    int ans = 0;
    REP(i,0,n) {
        REP(j,0,7) x[j]=0;
        REP(j,1,n) y[a[j]]=0;
        REP(j,1,i) { 
            x[a[j]%7]=max(x[a[j]%7],dp[i][j]);
            y[a[j]]=max(y[a[j]],dp[i][j]);
        }
        REP(j,i+1,n) {
            dp[i][j]=max(dp[i][0],max(y[a[j]+1],y[a[j]-1]));
            dp[i][j]=max(dp[i][j],x[a[j]%7]);
            dp[j][i]=++dp[i][j];
            x[a[j]%7]=max(x[a[j]%7],dp[i][j]);
            y[a[j]]=max(y[a[j]],dp[i][j]);
            ans = max(ans, dp[i][j]);
        }
    }
    printf("%d\n", ans);
}

 

Two Melodies CodeForces - 813D

标签:for   转移   size   ble   pre   problem   class   http   mes   

原文地址:https://www.cnblogs.com/uid001/p/10217547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!