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 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
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 xor 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.

想到了dp 但不知道咋下手

后来看了下题解  就是预处理+基础dp

题意是是分成区间 一个区间要有所有的a[i]  求这些区间的最大的异或和(重复不计)

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const int MAX=+;
struct node{
int x,y;
}a[N];
int v[N];
int dp[N];
int vis[N];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d",&v[i]);
if(a[v[i]].x==)a[v[i]].x=i;
else
a[v[i]].y=i;
}
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
dp[i]=dp[i-];
int ans=;
int minn=i;
for(int j=i;j>=;j--){
int t=v[j];
if(vis[t]==){
if(a[t].y>i)break;
minn=min(minn,a[t].x);
ans=ans^t;
vis[t]=;
}
if(j<=minn)dp[i]=max(dp[i],dp[j-]+ans);
}
}
cout<<dp[n]<<endl;
}
}

CodeForces - 811C Vladik and Memorable Trip(dp)的更多相关文章

  1. Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)

    <题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...

  2. CodeForces 811C Vladik and Memorable Trip

    $dp$. 记录$dp[i]$表示以位置$i$为结尾的最大值. 枚举最后一段是哪一段,假设为$[j,i]$,那么可以用$max(dp[1]...dp[j-1]) + val[j][i]$去更新$dp[ ...

  3. C. Vladik and Memorable Trip DP

    C. Vladik and Memorable Trip time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...

  5. cf 811c Vladik and Memorable Trip

    原题链接:http://codeforces.com/contest/811/problem/C 题意:将数组中的连续数字连成若干个“线段”(或者不连),其实就是区间.区间必须满足对于其中的任意数字, ...

  6. Codeforces 811 C. Vladik and Memorable Trip

    C. Vladik and Memorable Trip   time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. C. Vladik and Memorable Trip 解析(思維、DP)

    Codeforce 811 C. Vladik and Memorable Trip 解析(思維.DP) 今天我們來看看CF811C 題目連結 題目 給你一個數列,一個區段的數列的值是區段內所有相異數 ...

  8. CodeForce-811C Vladik and Memorable Trip(动态规划)

    Vladik and Memorable Trip CodeForces - 811C 有一个长度为 n 的数列,其中第 i 项为 ai. 现在需要你从这个数列中选出一些互不相交的区间,并且保证整个数 ...

  9. codeforces 811 C. Vladik and Memorable Trip(dp)

    题目链接:http://codeforces.com/contest/811/problem/C 题意:给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...

随机推荐

  1. 02--Tomcat总体结构分析一

    注:此文章大部分参考大神文档,并且结合自身理解,补充了其他相关知识,谢绝转载.      大神原文地址链接:http://www.ibm.com/developerworks/cn/java/j-lo ...

  2. chinason工作室-兄弟的工作室开张了,欢迎来访喔!

    Chinason工作室,团队成员由多位多年从事软件开发及大型生产企业系统维护的工程师组成,借重传统国外协同软件的开发经验,结合国内企业实际需求,致力于本土企业工作流软件研发,workflow系统定制开 ...

  3. python发送文本邮件

    #!/usr/bin/env python #coding=utf-8 #Author: Ca0Gu0 import time import smtplib from email.mime.text ...

  4. (转)分布式文件存储FastDFS(六)FastDFS多节点配置

    http://blog.csdn.net/xingjiarong/article/details/50759918 前面几篇关于FastDFS的博客中介绍了如何在一台机器上搭建一个简易的FastDFS ...

  5. 零基础学习Python培训,应该选择哪个培训班?

    近几年中,Python一直是市场上最受欢迎的编程语言之一.它语法自然,入门简单,同时应用范围又极广,无论是大火的人工智能.大数据还是传统的web开发.自动化运维,Python都能够大展拳脚.根据职友集 ...

  6. 基于owncloud构建私有云储存网盘

    注意事项:需要ping通外网 需要LAMP架构yum -y install httpd php php-mysql mariadb-server mariadb sqlite php-dom php- ...

  7. ASP.NET大闲话:ashx文件有啥用

    在VS中右击项目,添加新项,我们找到.ashx文件在新建项模板中叫做“一般处理程序”,那么这个一般处理程序用来干吗的呢? 我们可以这样地简单理解,嗯,不需搞得太复杂,它就类似.aspx文件,用于处理传 ...

  8. 第一节:web爬虫之requests

    Requests库是用Python编写的,并且Requests是一个优雅而简单的Python HTTP库,在使用Requests库时更加方便,可以节约我们大量的工作,完全满足HTTP测试需求.

  9. zend studio【快捷键】

    =================================[快捷键 zend studio]========== 1.调出查找面板[ctrl+f] 2.全文检索[ctrl+h] 3.关闭当前文 ...

  10. HTML的基本操作学习----常用标签,特殊符号,列表,表格,表单

    什么是HTML 常用标签 标题标签 段落标签 粗体标签+斜体 超链接标签 图片标签 列表标签 无序标签 有序标签 自定义列表 div标签 特殊符号 表格 表单 HTML 什么是 HTML?   HTM ...