Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
来源:互联网 发布:淘宝客推广收费吗 编辑:程序博客网 时间:2024/06/09 14:27
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 codeai 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 isnot 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 cityx, 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 cityx, 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 positionl to position r is equal toXOR of all distinct codes of cities for people on the segment from positionl 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 integersa1, a2, ..., an (0 ≤ ai ≤ 5000), where ai denotes code of the city to whichi-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
In the first test case best partition into segments is:[4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2xor 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.
题意: 给一个有n个人排队上车,去相同地方的人要么坐在同一个车厢,要不就不上车,问最大苏适度和是多少。苏适度是车厢内所有数组成的集合的抑或值。
分析: 因为同样数字的人要么上同一个车厢要不就不计算,所以在枚举划分车厢之前要先进行预处理。
处理出所有可行区间,然后计算出所有情况的xor,进行dp。
处理可行区间的方法,查看当前区间内含有的每种数的数量是否等于它们自己本身的总数量。
#include<stdio.h>#include<string.h>#include<map>#include<vector>#include<algorithm>using namespace std;int a[55555];//map<int,int>q,p;int q[55555];int p[55555];int vis[55555];vector<int>v[55555];int dp[55555];int mp[5555][5555];int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++){v[i].clear();scanf("%d",&a[i]);q[a[i]]++; }memset(dp,0,sizeof(dp));int t=1;for(int i=1;i<=n;i++){int sum=0,cnt=0;memset(vis,0,sizeof(vis));memset(p,0,sizeof(p));for(int j=i;j<=n;j++){p[a[j]]++;if(q[a[j]]==p[a[j]]) cnt++;if(!vis[a[j]]) sum++,vis[a[j]]=1;if(sum==cnt)v[j].push_back(i);}}for(int i=1;i<=n;i++){int xorr=0;memset(vis,0,sizeof(vis));for(int j=i;j<=n;j++){if(!vis[a[j]]) xorr^=a[j],vis[a[j]]=1;mp[i][j]=xorr;}}for(int i=1;i<=n;i++){dp[i]=dp[i-1];for(int j=0;j<v[i].size();j++){dp[i]=max(dp[i-1],dp[v[i][j]-1]+mp[v[i][j]][i]);}}printf("%d\n",dp[n]);}
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- 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 C. Vladik and Memorable Trip (DP)题解
- codeforces C. Vladik and Memorable Trip DP
- #416 Div.2 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
- codeforces 811C Vladik and Memorable Trip
- CF811C:Vladik and Memorable Trip(dp)
- Vladik and Memorable Trip CodeForces
- C. Vladik and Memorable Trip
- codeforces 811C C. Vladik and Memorable Trip
- C. Vladik and Memorable Trip dp 选择区间
- Redis简单命令
- JAVA中关于多线程的理解
- 一次base64编码的惨案
- c语言程序2
- C盘是系统盘,重装系统在了D盘然后出现了错误
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- Codeforces Round #416 (Div. 2)C. Vladik and Memorable Trip
- PHP程序员面试题
- Largest prime factor(筛法求素数)
- 北大ACM1001心得(1)——大数加法
- android 实现圆形进度条
- redis应用场景介绍
- 计数排序
- 修改Eclipse默认编译输出目录