#416 Div.2 C. Vladik and Memorable Trip

来源:互联网 发布:中国家居建材 知乎 编辑:程序博客网 时间:2024/06/05 22:48

题目链接:
http://codeforces.com/contest/811/problem/C

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

大意:

给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间。
每个区间的价值为该区间不同的数的异或值,现在问你这n个数最大的价值是多少。

一看就感觉是 DP,然后就没下文了…太菜了…
首先肯定要预处理 得到这个数第一次出现和最后一次出现的位置 。
其实就是个普通的 DP

#include<bits/stdc++.h>using namespace std;typedef long long ll;#define mem(s,v) memset(s,v,sizeof(s))#define D(v) cout<<#v<<" "<<v<<sel#define inf 0x3f3f3f3fint a[5010],fi[5010],se[5010],dp[5010],vis[5010];int main(){    int n;    scanf("%d",&n);    mem(a,0);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(!fi[a[i]]) fi[a[i]]=i;        se[a[i]]=i;    }    for(int i=1;i<=n;i++){        dp[i]=dp[i-1];        mem(vis,0);        int st=i,res=0;        for(int j=i;j;j--){            if(!vis[a[j]]){                if(se[a[j]]>i) break;                if(fi[a[j]]<st) st=fi[a[j]];                res^=a[j];vis[a[j]]=1;            }            if(j<=st) dp[i]=max(dp[i],dp[j-1]+res);//维护区间之前的值        }    }    printf("%d\n",dp[n]);    return 0;}
阅读全文
1 0