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.
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.
The output should contain a single integer — maximal possible total comfort.
6 4 4 2 5 2 3
14
9 5 1 3 1 5 2 4 2 5
9
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.
题意:
有一个序列 把它划分成若干个不相交区间 每个区间的贡献是这个区间中不同的数的异或值 如果这个区间中的某个数在序列中有相同的数不在这个区间中 那么这个区间不合法 求将这个序列用不相交的合法区间填充(可以不填满)的最大贡献。
题解:
用cnt[i][j]来存从i-j中区间不同的数的异或值 然后用一个bool 数组来表示i-j这个区间是否合法 然后就可以dp了
(2017.5.28)其实并不用开二维数组 只要在计算的时候顺便更新f就可以把空间复杂度降到O(n)。
方程:
f[i]=max(f[i-1],f[j-1]+cnt[i][j](i-j合法));
时空复杂度O(n^2);
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5001;
bool ca[maxn][maxn]={0};
int X[maxn][maxn];
int st[maxn],en[maxn];
bool vis[maxn];
int f[maxn]={0};
int A[maxn];
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
memset(st,127,sizeof(st));
memset(en,-127,sizeof(en));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&A[i]);
st[A[i]]=min(i,st[A[i]]);
en[A[i]]=max(i,en[A[i]]);
}
int stamin,endmax;
for(int i=1;i<=n;i++){
stamin=0x7fffffff,endmax=-0x7fffffff;
int x=0;
memset(vis,0,sizeof(vis));
for(int j=i;j<=n;j++){
stamin=min(stamin,st[A[j]]);
endmax=max(endmax,en[A[j]]);
if(stamin>=i&&endmax<=j)
ca[i][j]=1;
if(!vis[A[j]]){
x^=A[j];
vis[A[j]]=1;
}
X[i][j]=x;
}
}
for(int i=1;i<=n;i++){
f[i]=max(f[i],f[i-1]);
for(int j=1;j<=i;j++){
if(ca[j][i]){
f[i]=max(f[i],f[j-1]+X[j][i]);
}
}
}
printf("%d\n",f[n]);
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5001;
//bool ca[maxn][maxn]={0};
//int X[maxn][maxn];
int st[maxn],en[maxn];
bool vis[maxn];
int f[maxn]={0};
int A[maxn];
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
memset(st,127,sizeof(st));
memset(en,-127,sizeof(en));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&A[i]);
st[A[i]]=min(i,st[A[i]]);
en[A[i]]=max(i,en[A[i]]);
}
int stamin,endmax;
for(int i=1;i<=n;i++){
f[i]=max(f[i],f[i-1]);
stamin=0x7fffffff,endmax=-0x7fffffff;
int x=0;
memset(vis,0,sizeof(vis));
for(int j=i;j<=n;j++){
stamin=min(stamin,st[A[j]]);
endmax=max(endmax,en[A[j]]);
if(!vis[A[j]]){
x^=A[j];
vis[A[j]]=1;
}
if(stamin>=i&&endmax<=j){
f[j]=max(f[j],f[i-1]+x);
}
}
}
/*for(int i=1;i<=n;i++){
f[i]=max(f[i],f[i-1]);
for(int j=1;j<=i;j++){
if(ca[j][i]){
f[i]=max(f[i],f[j-1]+X[j][i]);
}
}
}*/
printf("%d\n",f[n]);
return 0;
}