zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    windows 乱码之 gbk 与 cp936
    jdcli 在命令行反编译jar包
    建议博客园向独立博客提供发布到首页的服务
    IsByRef在什么情况下为true?
    Hibernate里自定义UserType时取不到值的问题
    解决安装Visual Studio 2010 SP1时被NDP40KB2468871.exe补丁卡死以及mscorsvw.exe进程CPU占用率高的问题
    FROM WAS7/JDK5 TO WAS6/JDK4
    C++山寨C#中的DataTable
    程序员的自我修养读书笔记
    Web开发之路
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6919328.html
Copyright © 2011-2022 走看看