Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip

地址:http://codeforces.com/contest/811/problem/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
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.

 

思路:

  首先明确:对于两个数ab,他们都只出现一次。分成两端肯定比分成一段好。因为a+b>=a^b

  主思路区间dp,dp[i]表示前i个数所能得到的最大值。

  dp[i]=max(dp[j]+f[j+1][i])。0<=j<i。

  ps:注意区间[j+1,i]必须是合法区间!

  附赠一组数据:

  4

  4 8 4 8

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=5e3+7;
12 const int mod=1e9+7;
13 
14 int s[K][K];
15 int n,a[K],dp[K],r[K],l[K];
16 priority_queue<int>q[K];
17 int sc(int x)
18 {
19     int mx=r[a[x]];
20     for(int i=x+1;i<=mx;i++)
21     if(l[a[i]]<x)
22         return 0;
23     else
24         mx=max(mx,r[a[i]]);
25     return mx;
26 }
27 int main(void)
28 {
29     scanf("%d",&n);
30     for(int i=1;i<=n;i++)
31         scanf("%d",a+i),r[a[i]]=i,!l[a[i]]?l[a[i]]=i:0;
32     for(int i=1;i<=n;i++)
33     {
34         int t=0;
35         for(int j=i;j<=n;j++)
36         {
37             int tt=a[j];
38             if(r[tt]==j&&l[tt]>=i)
39             {
40                 t^=a[j];
41             }
42             s[i][j]=t;
43             //printf("%d %d:%d
",i,j,s[i][j]);
44         }
45     }
46     for(int i=1;i<=n;i++)
47     {
48         int mx=0,rr=sc(i);
49         if(!rr) continue;
50         for(int j=1;j<i;j++)
51         if(q[j].size())
52             mx=max(q[j].top(),mx);
53         q[rr].push(mx+s[i][rr]);
54     }
55     int ans=0;
56     for(int i=1;i<=n;i++)
57     if(q[i].size())
58         ans=max(ans,q[i].top());
59     printf("%d
",ans);
60     return 0;
61 }
62 /*
63 4
64 4 8 4 8
65 */

 

原文地址:https://www.cnblogs.com/weeping/p/6924265.html