C. Vladik and Memorable Trip DP

本文介绍了一种算法,用于解决火车车厢安排问题,目标是在给定乘客目的地的情况下,通过合理划分车厢来最大化整个行程的舒适度。算法采用动态规划方法,考虑了乘客目的地的唯一性和区间段的XOR运算。
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 10^3 的时候基本就不是回溯法

从前到后 由前面的状态更新后面的(刷表法)

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 5005
#define MOD 1000000
#define INF 1000000009
#define eps 0.00000001
using namespace std;

/*
dp[i]表示元素a[i]之前的最大comfort
*/
int dp[MAXN], l[MAXN], r[MAXN], a[MAXN], n;
bool been[MAXN];
int main()
{
    scanf("%d", &n);
    memset(l, INF, sizeof(l));
    memset(r, -INF, sizeof(r));
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        l[a[i]] = min(i, l[a[i]]);
        r[a[i]] = max(i, r[a[i]]);
    }
    for (int i = 0; i <= n; i++)
        dp[i] = -INF;
    dp[0] = 0;
    for (int i = 0; i < n; i++)
        if (dp[i] != -INF)
        {
            dp[i + 1] = max(dp[i + 1], dp[i]);
            int L = i, R = i, sum = 0;
            memset(been, false, sizeof(been));
            for (int j = i; j <= R; ++j)
            {
                L = min(L, l[a[j]]);
                R = max(R, r[a[j]]);
                if (!been[a[j]])
                {
                    sum ^= a[j];
                    been[a[j]] = true;
                }
            }
            if (L == i)
                dp[R + 1] = max(dp[R + 1], dp[i] + sum);
        }
    printf("%d\n", dp[n]);
    return 0;
}

 

 

转载于:https://www.cnblogs.com/joeylee97/p/6930225.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值