Codeforces Round #416 C. Vladik and Memorable Trip (DP)题解

来源:互联网 发布:dd3000软件 编辑:程序博客网 时间:2024/05/20 14:43

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 xor 5) + 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.


选连续的一段乘客进入车厢,若选中的序列中有乘客A,A要去的地方id为x,则其他去x的乘客也得被选上,可以不把乘客安排上车厢,求最大化(车厢内乘客城市编号异或值之和)。

找出所有合法的序列,然后DP就可以。

#include<iostream>#include<cstdio>#include<string>#include<algorithm>#include<string.h>using namespace std;//typedef long long ll;const int maxn = 5005;const int maxm = 10005;const int mod = 1000000009;int n, m;int num[maxn];int ll[maxn], rr[maxn];int dp[maxn][maxn];bool vis[maxn];bool fen[maxn];int val[maxn];int main(){    int n;    scanf("%d", &n);    for (int i = 1; i <= n; i++){        scanf("%d", &num[i]);    }    for (int i = 1; i <= n; i++){//寻找去这个城市的序列最左端        int cc = num[i];        if (ll[cc] == 0)ll[cc] = i;    }    for (int i = n; i > 0; i--){//寻找去这个城市的序列最右端        int cc = num[i];        if (rr[cc] == 0)rr[cc] = i;    }    for (int i = 1; i <= n; i++){//预处理出合法的序列以及异或值        if (ll[num[i]] == i){            fen[i] = 1;            memset(vis, 0, sizeof(vis));            for (int j = i; j <= rr[num[i]]; j++){                if (!vis[num[j]]){                    vis[num[j]] = 1;                    val[i] ^= num[j];                }                if (ll[num[j]] < i){ fen[i] = 0; break; }                if (rr[num[j]]>rr[num[i]])rr[num[i]] = rr[num[j]];            }        }    }    for (int k = 1; k <= n; k++){        for (int i = 1; i + k - 1 <= n; i++){            int j = i + k - 1;            if (fen[i]&&rr[num[i]] <= j){//可以分段                dp[i][j] = max(val[i] + dp[rr[num[i]]+1][j], dp[i+1][j]);            }            else{                dp[i][j] = max(dp[i][j], dp[i + 1][j]);            }        }    }    printf("%d\n", dp[1][n]);    return 0;}