C. Vladik and Memorable Trip
来源:互联网 发布:小学一年级数学软件 编辑:程序博客网 时间:2024/06/09 16:58
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
如今菜的连线性dp也想不出来。QAQ
对于当前点,我们不断往前看是否能构成一个新的区间,如果能够成dp[i] = max(dp[i],dp[j-1]+res);这里j代表的就是左端点,res表示新构成的区间的不同的数的异或值。
另外一个情况就是默认不选择当前这个点,那么dp[i] = dp[i-1]。
#include <bits/stdc++.h>using namespace std;const int MAXN = 5e3+7;const int inf =1e9;int n;int num[MAXN],dp[MAXN];int first_pos[MAXN],last_pos[MAXN];bool vis[MAXN];int main(){ scanf("%d",&n); for(int i = 1 ; i <= n ; ++i) { scanf("%d",&num[i]); if(!first_pos[num[i]])first_pos[num[i]] = i; last_pos[num[i]] = i; } for(int i = 1 ; i <= n ; ++i) { dp[i] = dp[i-1]; memset(vis,0,sizeof(vis)); int st = first_pos[num[i]]; int res = 0; for(int j = i ; j >= 1 ; --j) { if(!vis[num[j]]) { if(last_pos[num[j]]>i)break; st = min(st,first_pos[num[j]]); res^=num[j]; vis[num[j]] = 1; } if(j<=st)dp[i] = max(dp[i] , dp[j-1]+res);//维护区间最值 } } printf("%d\n",dp[n]);}
- C. Vladik and Memorable Trip
- codeforces 811C Vladik and Memorable Trip
- CF 811C. Vladik and Memorable Trip
- codeforces C. Vladik and Memorable Trip DP
- Codeforces811C. Vladik and Memorable Trip
- Vladik and Memorable Trip CodeForces
- codeforces 811C C. Vladik and Memorable Trip
- Codeforces Round #416 (Div. 2)C. Vladik and Memorable Trip
- #416 Div.2 C. Vladik and Memorable Trip
- Codeforces Round #416 C. Vladik and Memorable Trip (DP)题解
- Codeforces 811C Vladik and Memorable Trip【Dp】
- C. Vladik and Memorable Trip dp 选择区间
- Codeforces Round #416 (Div. 2)-C. Vladik and Memorable Trip
- Codeforces 811C Vladik and Memorable Trip[dp]
- Codeforces 811C Vladik and Memorable Trip DP
- Codeforces Round #416 (Div. 2) C Vladik and Memorable Trip
- CF811C:Vladik and Memorable Trip(dp)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- 二叉树的层次遍历
- POJ-3126 Prime Path
- USACO——Paired Up
- LINUX字符设备驱动纠正
- MySQL数据库操作
- C. Vladik and Memorable Trip
- 图像处理基础知识系列之二:核概率密度估计简介
- 网站怎么赚钱? 靠流量赚钱吗? 广告吗? 建什么网站赚钱
- scala隐式转换
- 在Nanopi-NEO上直接编译带Cedrus-avcodec的ffmpeg
- Mybatis之Mapper动态代理方式
- 1802 Set Operations (eden)
- MySQL整数类型
- SSM 配置ehcache