Two Melodies CodeForces - 813D

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);
}

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

时间: 2024-08-29 15:58:19

Two Melodies CodeForces - 813D的相关文章

待 题表

题表 达哥终极杂题表Bzoj2839 hdu6021 Codeforces 804DBzoj2248 hdu5575 Codeforces 786CBzoj2013 bzoj2676 Codeforces 803CBzoj2386 bzoj3782 Codeforces 813DBzoj2699 cogs1667 Codeforces 814DBzoj4798 bzoj2064 Codeforces 814EBzoj4639 bzoj3505 Codeforces 815ABzoj4417 bz

codeforces:818G Four Melodies分析

题目 题目大意是有一组自然数v1,...,vn,要求在其中找到四个非空子序列(从原来的自然数序列中挑选一部分数,并按原先后关系排序),这些子序列互不相交,且每个子序列中的前后元素的值要么差值的绝对值为1,要么对7取余的值相同. 输入自然数序列的长度n满足4<=n<=3000,而每个输入的自然数均不超过1e5. 求解所有满足以上条件的这样四个子序列的长度的总和的最大可能值.  预处理 这道题有点难度,考察的知识范围很大.我个人也是参考了codeforces上的tutorial.要解决这个问题必须

Educational Codeforces Round 24 CF 818 A-G 补题

6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子,一直坚持刷题,这一个学期刷了200道吧,感觉还是小有收获.特别是Ural和Codeforces上的题,质量很高. 然后4月的校赛,5月的省赛,发挥的也一般,不过也没有太失常. 希望暑假的选拔赛能碰到有趣的队友 蛤蛤. 这两天各种考试,实在是太忙,看了一下edu24的题目,不是很容易,做了一道水题,以

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp