• CodeForces


    C. Vladik and Memorable Trip
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 r. XOR 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.

    Examples
    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: 5 1 [3] 1 5 [2, 4, 2] 5, answer calculated as follows: 3 + (2 xor 4) = 3 + 6 = 9.

    想到了dp 但不知道咋下手

    后来看了下题解  就是预处理+基础dp

    题意是是分成区间 一个区间要有所有的a[i]  求这些区间的最大的异或和(重复不计)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cctype>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<map>
     8 #include<stack>
     9 #include<set>
    10 #include<vector>
    11 #include<algorithm>
    12 #include<string.h>
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int INF=0x3f3f3f3f;
    17 const double eps=0.0000000001;
    18 const int N=500000+10;
    19 const int MAX=1000+10;
    20 struct node{
    21     int x,y;
    22 }a[N];
    23 int v[N];
    24 int dp[N];
    25 int vis[N];
    26 int main(){
    27     int n;
    28     while(scanf("%d",&n)!=EOF){
    29         memset(dp,0,sizeof(dp));
    30         memset(a,0,sizeof(a));
    31         memset(vis,0,sizeof(vis));
    32         for(int i=1;i<=n;i++){
    33             scanf("%d",&v[i]);
    34             if(a[v[i]].x==0)a[v[i]].x=i;
    35             else
    36                 a[v[i]].y=i;
    37         }
    38         for(int i=1;i<=n;i++){
    39             memset(vis,0,sizeof(vis));
    40             dp[i]=dp[i-1];
    41             int ans=0;
    42             int minn=i;
    43             for(int j=i;j>=1;j--){
    44                 int t=v[j];
    45                 if(vis[t]==0){
    46                     if(a[t].y>i)break;
    47                     minn=min(minn,a[t].x);
    48                     ans=ans^t;
    49                     vis[t]=1;
    50                 }
    51                 if(j<=minn)dp[i]=max(dp[i],dp[j-1]+ans);
    52             }
    53         }
    54         cout<<dp[n]<<endl;
    55     }
    56 }
  • 相关阅读:
    支付宝沙箱测试
    SQLServer2012书学习结束
    SQLServer2012书学习十 十一 十二 存储过程、触发器、游标
    SQLServer2012书学习七八九 统计数据、视图、索引
    SQLServer2012书学习第五六
    一些工具的定义
    SQLServer2012书学习第四章
    SQLServer2012书学习
    SqlServer文件组
    数据库硬盘满了如何清空数据库日志
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6919328.html
Copyright © 2020-2023  润新知