zoukankan      html  css  js  c++  java
  • (dp)CF 813 Educational Codeforces Round 22 D. Two Melodies

    D. Two Melodies
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time!

    Alice has a sheet with n notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal.

    Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

    Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7.

    You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.

    Input

    The first line contains one integer number n (2 ≤ n ≤ 5000).

    The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.

    Output

    Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.

    Examples
    input
    4
    1 2 4 5
    output
    4
    input
    6
    62 22 60 61 48 49
    output
    5
    Note

    In the first example subsequences [1, 2] and [4, 5] give length 4 in total.

    In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.

    思路见代码注释。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <vector>
    12 #include <stack>
    13 #define mp make_pair
    14 //#define P make_pair
    15 #define MIN(a,b) (a>b?b:a)
    16 //#define MAX(a,b) (a>b?a:b)
    17 typedef long long ll;
    18 typedef unsigned long long ull;
    19 const int MAX=5e3+5;
    20 const int MAX_V=1e3+5;
    21 const ll INF=4e18+5;
    22 const double M=4e18;
    23 using namespace std;
    24 const int MOD=1e9+7;
    25 typedef pair<ll,int> pii;
    26 const double eps=0.000000001;
    27 #define rank rankk
    28 int dp[MAX][MAX];//dp[i][j]表示第一个序列中最后一个取i,另一个序列最后一个取j的最大长度
    29 int maxmod[10],maxnum[(int)(1e5+5)];//分别记录i一定,进行到当前j时,模7为若干或a[x]=多少时最大的dp[i][?]
    30 int a[MAX];
    31 int n,an;
    32 int main()
    33 {
    34     scanf("%d",&n);
    35     for(int i=1;i<=n;i++)
    36         scanf("%d",&a[i]);
    37     for(int i=0;i<=n;i++)//枚举第一个序列的情况
    38     {
    39         memset(maxmod,0,sizeof(maxmod));
    40         memset(maxnum,0,sizeof(maxnum));
    41         for(int j=1;j<=n;j++)
    42         {
    43             if(i==j)
    44                 dp[i][j]=0;//i=j构不成两个非空不重合的序列,故为0
    45             else
    46             {
    47                 int mod=a[j]%7;
    48                 if(i>j)//i>j的情况转化为已算过的j<i
    49                 {
    50                     maxmod[mod]=max(maxmod[mod],dp[i][j]);
    51                     maxnum[a[j]]=max(maxnum[a[j]],dp[i][j]);
    52                     continue;
    53                 }
    54                 dp[i][j]=max(maxmod[mod]+1,maxnum[a[j]-1]+1);//前一个为mod7同余 或 前一个为a[j]-1 的比较
    55                 dp[i][j]=max(dp[i][j],maxnum[a[j]+1]+1);//与前一个为a[i]+1的比较
    56                 dp[i][j]=max(dp[i][j],dp[i][0]+1);//新起一个序列
    57                 dp[j][i]=dp[i][j];
    58                 maxmod[mod]=max(maxmod[mod],dp[i][j]);
    59                 maxnum[a[j]]=max(maxnum[a[j]],dp[i][j]);
    60                 an=max(an,dp[i][j]);
    61             }
    62         }
    63     }
    64     printf("%d
    ",an);
    65     return 0;
    66 }
  • 相关阅读:
    tensorflow入门笔记(五) name_scope和variable_scope
    tensorflow入门笔记(四) tf.summary 模块
    tensorflow入门笔记(三) tf.GraphKeys
    tensorflow入门笔记(二) 滑动平均模型
    tensorflow入门笔记(一) tf.app.flags.FLAGS
    iOS友盟推送注意点
    关于gif图片重用消失问题。
    百度地图使用经验
    SQLite语句
    app登录,client和serve的常用字段。
  • 原文地址:https://www.cnblogs.com/quintessence/p/6956674.html
Copyright © 2011-2022 走看看