CodeForces - 811C

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.

Example
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.

这个题差点没坑死我。。
主要的坑点在于他的区间可能是交叉的。。
所以要详细进行判断
比如先找好了当前左右以后
要扫面区间去找一下是不是真的左右
如果不是就只能继承前一个的值
否则才能择优。。

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
#include<cstdio>
using namespace std;
int tu[7001];
int n,dp[50001];
pair<int, int>fs[50001];
int gg = 0;
int main()
{
    cin >> n;
    for (int a = 1; a <= n; a++)cin >> tu[a];
    for (int a = 0; a <= 10000; a++)fs[a].first = -1;
    for (int a = 1; a <= n; a++)
    {
        if (fs[tu[a]].first == -1)
        {
            fs[tu[a]].first = a;
            fs[tu[a]].second = a;
            continue;
        }
        fs[tu[a]].second = max(fs[tu[a]].second, a);
    }
    dp[0] = 0;
    map<int, int>mp;
    for (int a = 1; a <= n; a++)
    {
        if (a != fs[tu[a]].second)
        {
            dp[a] = dp[a - 1];
            continue;
        }
        int z = fs[tu[a]].first;
        int gs = 0;
        int y = a;
        for (int b = a; b >= z; b--)
        {
            z = min(z, fs[tu[b]].first);
            y = max(y, fs[tu[b]].second);
        }
        if (y != a)
        {
            dp[a] = dp[a - 1];
            continue;
        }
        for (int b = y; b >= z; b--)
        {
            if (!mp[tu[b]])
            {
                gs ^= tu[b];
                mp[tu[b]] = 1;
                continue;
            }
        }
        mp.clear();
        dp[a] = max(dp[a], dp[a - 1]);
        dp[a] = max(dp[a], dp[z - 1] + gs);
    }
    cout << dp[n];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值