zoukankan      html  css  js  c++  java
  • (dp) Codeforces Round #416 (Div. 2)

    Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:

    Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known (the code of the city in which they are going to).

    Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can’t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.

    Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position rXOR operation also known as exclusive OR.

    Total comfort of a train trip is equal to sum of comfort for each segment.

    Help Vladik to know maximal possible total comfort.

    Input

    First line contains single integer n (1 ≤ n ≤ 5000) — number of people.

    Second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 5000), where ai denotes code of the city to which i-th person is going.

    Output

    The output should contain a single integer — maximal possible total comfort.

    Example

    Input
    6
    4 4 2 5 2 3
    Output
    14
    Input
    9
    5 1 3 1 5 2 4 2 5
    Output
    9

    Note

    In the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 xor 5) + 3 = 4 + 7 + 3 = 14

    In the second test case best partition into segments is: [3] [2, 4, 2] 5, answer calculated as follows: 3 + (2 xor 4) = 3 + 6 = 9.

    用dp[i]表示只用[1,i]区间的数按题目要求能构成的最大值。

    对于i+1,若不取a[i+1] 则dp[i+1]=dp[i]

    若取i+1,从i+1向左看,不断更新“当前区间中的数第一次出现的位置中的最小值”,并且如果某一位置的数最后一次出现的下标大于i+1就break。每当在“当前区间中的数第一次出现的位置中的最小值”左侧时更新dp[i+1]的值。

     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 MIN(a,b) (a>b?b:a)
    15 #define rank rankk
    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 INF=2147483647;
    21 const int B=1024;//桶的大小
    22 const double M=4e18;
    23 using namespace std;
    24 const int MOD=1e9+7;
    25 typedef pair<int,int> pii;
    26 int st[MAX],en[MAX],dp[MAX],num[MAX];
    27 bool vi[MAX];
    28 int n,tem,re,qian;
    29 int main()
    30 {
    31     scanf("%d",&n);
    32     for(int i=1;i<=n;i++)
    33     {
    34         scanf("%d",&tem);
    35         num[i]=tem;
    36         if(!st[tem])
    37             st[tem]=i;
    38         en[tem]=i;
    39     }
    40     for(int i=1;i<=n;i++)
    41     {
    42         tem=num[i];
    43         dp[i]=dp[i-1];
    44         re=0;
    45         memset(vi,false,sizeof(vi));
    46         int j;
    47         qian=st[num[i]];
    48         for(j=i;j>=1;j--)
    49         {
    50             if(en[num[j]]>i)
    51                 break;
    52             else if(!vi[num[j]])
    53             {
    54                 qian=min(qian,st[num[j]]);
    55                 vi[num[j]]=true;
    56                 re^=num[j];
    57             }
    58             if(j<=qian)
    59                 dp[i]=max(dp[i],dp[j-1]+re);
    60         }
    61     }
    62     printf("%d
    ",dp[n]);
    63 }
  • 相关阅读:
    再来五道剑指offer题目
    高强度学习训练第十天总结:Class文件
    windows linux 子系统及windows terminal的使用。
    从植物大战僵尸开始一步一步带你入门逆向工程,
    高强度学习训练第九天总结:5道剑指offer的题目
    高强度学习训练第八天总结:MySQL的一些优化
    JVM的一些工具的简要使用
    手把手教你使用Java实现一个神经网络
    指定路径创建文件,并写入数据
    c#创建windows服务(代码方式安装、启动、停止、卸载服务)
  • 原文地址:https://www.cnblogs.com/quintessence/p/6918148.html
Copyright © 2011-2022 走看看