博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 811C Vladik and Memorable Trip[补]
阅读量:5974 次
发布时间:2019-06-19

本文共 3101 字,大约阅读时间需要 10 分钟。

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个数字,对于每个数字a[i]你可以选择选或者不选,如果选,那么所有与a[i]相同数字构成一个区间,他的价值是该区间内不同数字异或。最后要求价值和最大。

分析:太菜,DP基本靠队友。对于这个问题,要想知道a[i]构成的区间,我们在输入时可以记录每一个a[i]的开始位置和结束位置(s[a[i]]为a[i]的开始位置,e[a[i]]为a[i]的结束位置)。题目要求区间[1,n]的最大值,我们可以转化为求[1,i]区间的最大值dp[i]的子问题,以此递推求dp[n],这就是基本DP。

AC代码:

1 #include
2 using namespace std; 3 4 int a[5005],s[5005],e[5005]; 5 int dp[5005],vis[5005]; 6 int main() 7 { 8 ios_base::sync_with_stdio(0); 9 cin.tie(0);10 int n;11 memset(s,0xff,sizeof(s));12 memset(dp,0,sizeof(dp));13 cin>>n;14 for(int i=1;i<=n;i++){15 cin>>a[i];16 if(s[a[i]]==-1){17 s[a[i]]=i;18 }19 e[a[i]]=i;20 }21 for(int i=1;i<=n;i++){22 dp[i]=dp[i-1];23 memset(vis,0,sizeof(vis));24 int st=i;25 int ans=0;26 for(int j=i;j>0;j--){27 if(vis[a[j]]==0){28 if(e[a[j]]>i) break;29 if(s[a[j]]
View Code

 

转载于:https://www.cnblogs.com/ls961006/p/6915377.html

你可能感兴趣的文章
TDD 的本质不是 TDD
查看>>
linux命令学习——ps
查看>>
freemark 判断list是否为空
查看>>
JS的一些扩展:String、StringBuilder、Uri
查看>>
solr的suggest模块
查看>>
2PHP页面缓存
查看>>
编译原理 LL1文法First集算法实现
查看>>
菜鸟学Linux命令:bg fg jobs命令 任务管理
查看>>
【Linux系统编程】 Linux系统调用概述
查看>>
SQL Server Reporting Services:无法检索应用程序文件。部署中的文件已损坏
查看>>
hive中partition如何使用
查看>>
查看mysql数据库版本方法总结
查看>>
大牛手把手教你做日历(建议你看看,你会有收获的)
查看>>
Django中的ORM
查看>>
iOS开发UI篇—Quartz2D使用(图片剪切)
查看>>
spring学习笔记(20)数据库事务并发与锁详解
查看>>
关于Simple_html_dom的小应用
查看>>
鲁肃:蚂蚁金服的三个梦想
查看>>
【springmvc+mybatis项目实战】杰信商贸-27.POI由HSSF升级为XSSF
查看>>
数学常数e的含义
查看>>