您的位置:首页 > 其它

codeforces811C——Vladik and Memorable Trip(动态规划)

2017-05-28 13:00 441 查看
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 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.

题意比较复杂,做起来很简单,,给你一个序列,从中选出若干段,可以不用覆盖整个序列,但是一段中必须包含全部的这些数字,比如有一段中包含2,那么它必须是包含全部的2,或者就都不包含。

我们预处理出每个数字第一个出现的位置和最后一个出现的位置。以及每个区间内不同数字的异或和。

dp[i]表示考虑到前i个人,最大值是多少。分情况讨论一下即可,具体看代码把。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define _ ios::sync_with_stdio(false)

const int MAXN = 5010;
const int INF = 0xfffffff;
typedef long long ll;

int n;
ll a[MAXN];
int l[MAXN],r[MAXN];
ll s[MAXN][MAXN];
int vis[MAXN];
ll dp[MAXN];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%I64d",a+i);
if(l[a[i]])
l[a[i]]=min(l[a[i]],i);
else
l[a[i]]=i;
if(r[a[i]])
r[a[i]]=max(r[a[i]],i);
else
r[a[i]]=i;
}
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
s[i][i]=a[i];
vis[a[i]]=1;
for(int j=i+1;j<=n;j++){
if(!vis[a[j]]){
s[i][j]=s[i][j-1]^a[j];
vis[a[j]]=1;
}else{
s[i][j]=s[i][j-1];
}
}
}
dp[0]=0;
for(int i=1;i<=n;i++){
int temp=a[i];
if(i==r[temp]){
int L=l[temp];
int ok=1;
for(int j=l[temp]+1;j<r[temp];j++){
if(r[a[j]]>i){
ok=0;
break;
}
L=min(l[a[j]],L);
}
if(ok)
dp[i]=max(dp[i-1],dp[L-1]+s[L][i]);
else
dp[i]=dp[i-1];
}else
dp[i]=dp[i-1];
}
printf("%I64d\n",dp
);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: