Codeforces Round #416 (Div. 2)-C. Vladik and Memorable Trip
来源:互联网 发布:华西电子盘软件下载 编辑:程序博客网 时间:2024/05/19 17:26
原题链接
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.
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.
The output should contain a single integer — maximal possible total comfort.
64 4 2 5 2 3
14
95 1 3 1 5 2 4 2 5
9
对于每个目的地求出数组中最左边和最右边人的索引node[].l, node[].r
#include <bits/stdc++.h>#define maxn 5005using namespace std;struct Node {int l, r;}node[maxn];int vis[maxn], num[maxn], dp[maxn];int main() {//freopen("in.txt", "r", stdin);int n;scanf("%d", &n);for(int i = 1; i <= n; i++) {scanf("%d", num+i);}for(int i = n; i >= 1; i--) {if(node[num[i]].r == 0) {node[num[i]].r = node[num[i]].l = i;}else node[num[i]].l = i;}for(int i = 1; i <= n; i++) {int ans = 0, mins = 100000;memset(vis, 0, sizeof(vis));for(int j = i; j >= 1; j--) {if(node[num[j]].r > i) break;mins = min(node[num[j]].l, mins);if(vis[num[j]] == 0) {ans ^= num[j];vis[num[j]] = 1;}if(j == mins) {dp[i] = max(dp[i], ans+dp[j-1]);}}dp[i] = max(dp[i], dp[i-1]);}cout << dp[n] << endl;return 0;}
- Codeforces Round #416 (Div. 2)C. Vladik and Memorable Trip
- Codeforces Round #416 (Div. 2)-C. Vladik and Memorable Trip
- Codeforces Round #416 (Div. 2) C Vladik and Memorable Trip
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip 【线性DP】
- Codeforces Round #416 C. Vladik and Memorable Trip (DP)题解
- #416 Div.2 C. Vladik and Memorable Trip
- codeforces 811C Vladik and Memorable Trip
- codeforces C. Vladik and Memorable Trip DP
- Vladik and Memorable Trip CodeForces
- C. Vladik and Memorable Trip
- codeforces 811C C. Vladik and Memorable Trip
- Codeforces 811C Vladik and Memorable Trip【Dp】
- Codeforces 811C Vladik and Memorable Trip[dp]
- Codeforces 811C Vladik and Memorable Trip DP
- CF 811C. Vladik and Memorable Trip
- Codeforces811C. Vladik and Memorable Trip
- NOJ2211解题笔记
- 单源最短路径(Dijkstra算法)
- 论考试的题目应该是多少年前的原题-解题报告
- 分析Java 堆
- 网络层次分析
- Codeforces Round #416 (Div. 2)-C. Vladik and Memorable Trip
- Kotlin编程之if语句,while循环,when表达式,for循环
- VS2013使用本地GIT管理源代码
- Problem D: 不同交通工具的速度
- 动态代理和IBinder机制
- Understanding glibc malloc
- 新接触-MongoDB、Docker
- getElementsByTagName用法示例之全选,不选,反选
- 顺序容器