[刷题]Codeforces 811C
来源:互联网 发布:小米note能用电信网络 编辑:程序博客网 时间:2024/05/16 00:56
哇咔咔,终于期末考完啦,终于放暑假啦,我终于又有时间学习啦!!(o゜▽゜)o☆
Description
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
input64 4 2 5 2 3output14input95 1 3 1 5 2 4 2 5output9
Note
In the first test case best partition into segments is:
In the second test case best partition into segments is:
Key
给一个n个元素(可含有重名元素)的序列,按照原来的顺序分割出若干段,要求所有重名元素要么全部出现在一个段里,要么全部不在段里。问分割出的所有段的价值和。其中每个段的价值为段内所有不同名元素按照名字的二进制异或。
明显的DP问题,但当时还是没写出来。。。果然一个月没写就生疏了。
当时就觉得这个异或有猫腻。什么意思呢?对于两个二进制数
先预处理一下,每种元素只需要知道其最左侧与最右侧的元素位置即可。
对于这个序列中的每一个元素:有选中与不选中(或无法选中)两种情况。若试图选中当前元素,则要求出包含此类元素的段的最小区间
Code
#include<iostream>#include<cstring>#include<algorithm>#define l(t) s[t][0]#define r(t) s[t][1]using namespace std;const int maxn = 5000 + 10;int n;int a[maxn];int s[maxn][2];int tv[maxn];int dp[maxn];bool vis[maxn];int main(){ memset(s, 0, sizeof(s)); memset(tv, 0, sizeof(tv)); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (!l(a[i])) l(a[i]) = i; r(a[i]) = i; } dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1]; if (r(a[i]) > i) continue; int val = a[i]; int ml = l(a[i]); memset(vis, 0, sizeof(vis)); vis[a[i]] = true; int j; for (j = i; j > ml; --j) { if (vis[a[j]]) continue; vis[a[j]] = true; if (r(a[j]) > i) break; if (l(a[j]) < ml) ml = l(a[j]); val ^= a[j]; } if (j == ml) dp[i] = max(dp[i - 1], val + dp[j - 1]); } cout << dp[n]; return 0;}
- [刷题]Codeforces 811C
- [刷题]Codeforces 794C
- [刷题]Codeforces 758C
- codeforces 811C Vladik and Memorable Trip
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- codeforces 811C C. Vladik and Memorable Trip
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- codeforces 148 div2 C
- codeforces 242C
- Codeforces-234C Weather
- codeforces 106C Buns
- TensorFlow系列(1)——建立线性回归模型
- 区块链参考网站
- 递归
- Python逻辑运算 VS Numpy数组逻辑运算
- 古文观止卷七_兰亭集序_王羲之
- [刷题]Codeforces 811C
- Java并发编程:Callable、Future和FutureTask
- Zedboard学习(二):zedboard的Linux下交叉编译环境搭建
- Java 7 新特性try-with-resources语句
- Number of Islands 岛屿个数
- Java中什么是反射
- 【剑指offer】题40:数组中只出现一次的数字
- 多进程、多线程服务器
- Hashtable和HashMap的区别