CodeForces

来源:互联网 发布:房卡游戏源码 编辑:程序博客网 时间:2024/05/17 02:47

题目来源:http://codeforces.com/problemset/problem/811/C

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
64 4 2 5 2 3
output
14
input
95 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 xor5) + 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时,f[i]表示1~i的所有选中区段的和的最大值,方程f[i]=max(f[i],f[j-1]+s[j][i])(j<i)(s为区段亦或值)。

提供一组数据:

所选区段为整个区段。故结果为0^1^2^3^4^5=1。

代码:

#include <bits/stdc++.h>using namespace std;int n;const int maxn=5001;int a[maxn];int l[maxn];int r[maxn];int cnt=0;int p[maxn];int s[maxn][maxn];int f[maxn];int vis[maxn];int main(){ios::sync_with_stdio(0);cin.tie(0);cin>>n;for(int i=1;i<=n;i++){cin>>a[i];cnt=max(cnt,a[i]);if(!l[a[i]])l[a[i]]=i;r[a[i]]=i;p[a[i]]++;}for(int i=1;i<=n;i++){int num=0;int tot=0;memset(vis,0,sizeof(vis));for(int j=i;j<=n;j++){if(!vis[a[j]]){num=num^a[j];tot++;}vis[a[j]]++;if(vis[a[j]]==p[a[j]])tot--;if(!tot)s[i][j]=num;}}for(int i=1;i<=n;i++){f[i]=f[i-1];for(int j=1;j<=i;j++)f[i]=max(f[i],f[j-1]+s[j][i]);}cout<<f[n];return 0;}


原创粉丝点击