zoukankan      html  css  js  c++  java
  • Codeforces 811 C. Vladik and Memorable Trip

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

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

     

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<cstring>
    #include<map>
    #include<stack>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<string.h>
    typedef long long ll;
    typedef unsigned long long LL;
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double eps=0.0000000001;
    const int N=500000+10;
    const int MAX=1000+10;
    struct node{
        int x,y;
    }a[N];
    int v[N];
    int dp[N];
    int vis[N];
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            memset(dp,0,sizeof(dp));
            memset(a,0,sizeof(a));
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++){
                scanf("%d",&v[i]);
                if(a[v[i]].x==0)a[v[i]].x=i;
                else a[v[i]].y=i;
            }
            for(int i=1;i<=n;i++){
                memset(vis,0,sizeof(vis));
                dp[i]=dp[i-1];
                int ans=0,minn=i;
                for(int j=i;j>=1;j--){
                    int t=v[j];
                    if(vis[t]==0){
                        if(a[t].y>i)break;
                        minn=min(minn,a[t].x);
                        ans=ans^t;
                        vis[t]=1;
                    }
                    if(j<=minn)
                dp[i]=max(dp[i],dp[j-1]+ans); } } cout<<dp[n]<<endl; } }
  • 相关阅读:
    正常安装selenium后,pycharm导入selenium失败
    python+selenium自动化的准备 2:安装python 3.7.4 和selenium 2.53.1
    python+selenium自动化的准备 1:安装浏览器(火狐)及浏览器插件firebug与firepath、selenium IDE
    虚拟机安装win10系统
    官网下载Windows 10 系统的iso镜像文件
    电脑系统属性中用户与系统环境变量的区别
    验证器
    EchoMode的显示效果
    文本框类控件
    QLabel标签快捷键的使用
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9692618.html
Copyright © 2011-2022 走看看