Codeforces811C. Vladik and Memorable Trip

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.

这题感觉还是曼坑的。。。我的思路是把每个数字看成一段区间,然后这段区间取还是不取,这段区间的权值是这个区间内不同数字的异或值。然后根据每个区间的左右端点 sort一下去dp。dp[i]表示取i这个区间可以取到的最大值,那么对每个满足转移条件的j 即 s[i].l > s[j].r 可以做一次dp[i] = max(dp[i],dp[j]+s[i].value) 我一开始A不了,后来想明白了。

对 54353对这个区间 我一开始的处理是对5这个数字分配到的区间是0-3,但是我们在区间里取了3,所以我们还要把外面那个3加进来,对于5这个数字他的实际区间应该是0-4。所以我们在预处理完区间之后再扩展一次区间,然后正常做就可以了。
//thanks to qhl ❤

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
using namespace std;

//thanks to pyf ...
//thanks to qhl ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 1e6 + 5;

struct Node
{
    int l, r;
    int value;
} s[10000];
int a[10000];
int vis[10000];
int dp[10000];
bool cmp(Node a, Node b)
{
    return a.r < b.r || (a.r == b.r && a.l < b.l);
}
int main()
{
    int n;
    while (cin >> n)
    {
        int cnt = 0;
        memset(dp, 0, sizeof(dp));
        map<int, int>m;
        m.clear();
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            a[i] = x;
            if (!m.count(x))
            {
                m[x] = cnt ++;
                s[m[x]].l = i;
                s[m[x]].r = i;
            }
            else
            {
                s[m[x]].l = min(s[m[x]].l, i);
                s[m[x]].r = max(s[m[x]].r, i);
            }
        }
        for (int i = 0; i < cnt; i++)
        {
            for (int j = s[i].l ; j <= s[i].r; j++)
            {
                s[i].l = min(s[m[a[j]]].l, s[i].l);
                s[i].r = max(s[m[a[j]]].r, s[i].r);
            }
        }
        for (int i = 0; i < cnt; i++)
        {
            memset(vis, 0, sizeof(vis));
            s[i].value = 0;
            for (int j = s[i].l; j <= s[i].r; j++)
            {
                if (!vis[a[j]])
                {
                    s[i].value ^= a[j];
                    vis[a[j]] = 1;
                }
            }
        }
        sort(s, s + cnt, cmp);
        for (int i = 0; i < cnt; i++)
        {
            dp[i] = s[i].value;
            for (int j = 0; j < i; j++)
            {
                if (s[i].l > s[j].r)
                    dp[i][1] = max(dp[j] + s[i].value, dp[i]);
            }
        }
        int ans = 0;
        for (int i = 0; i < cnt; i++)
            ans = max(ans, dp[i]);
        cout << ans << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值