codeforces 811C 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 xor5) + 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

题意:给你n个数字,对于每个数字a[i]你可以选择选或者不选,如果选,那么所有与a[i]相同数字构成一个区间,他的价值是该区间内不同数字异或。最后要求价值和最大。

分析:太菜,DP基本靠队友。对于这个问题,要想知道a[i]构成的区间,我们在输入时可以记录每一个a[i]的开始位置和结束位置(s[a[i]]为a[i]的开始位置,e[a[i]]为a[i]的结束位置)。题目要求区间[1,n]的最大值,我们可以转化为求[1,i]区间的最大值dp[i]的子问题,以此递推求dp[n],这就是基本DP。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int a[5005],s[5005],e[5005];
 5 int dp[5005],vis[5005];
 6 int main()
 7 {
 8     ios_base::sync_with_stdio(0);
 9     cin.tie(0);
10     int n;
11     memset(s,0xff,sizeof(s));
12     memset(dp,0,sizeof(dp));
13     cin>>n;
14     for(int i=1;i<=n;i++){
15         cin>>a[i];
16         if(s[a[i]]==-1){
17             s[a[i]]=i;
18         }
19         e[a[i]]=i;
20     }
21     for(int i=1;i<=n;i++){
22         dp[i]=dp[i-1];
23         memset(vis,0,sizeof(vis));
24         int st=i;
25         int ans=0;
26         for(int j=i;j>0;j--){
27             if(vis[a[j]]==0){
28                 if(e[a[j]]>i) break;
29                 if(s[a[j]]<st) st=s[a[j]];
30                 ans=ans^a[j];
31                 vis[a[j]]=1;
32             }
33             if(j<=st){
34                 dp[i]=max(dp[i],dp[j-1]+ans);
35             }
36         }
37     }
38     cout<<dp[n]<<endl;
39 
40 }
View Code

 

posted @ 2017-05-28 11:40  BadboyQAQ  阅读(554)  评论(0编辑  收藏  举报