C. Vladik and Memorable Trip DP
2 seconds
256 megabytes
standard input
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.
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.
6
4 4 2 5 2 3
14
9
5 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 + (2 xor5) + 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 10^3 的时候基本就不是回溯法
从前到后 由前面的状态更新后面的(刷表法)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 5005
#define MOD 1000000
#define INF 1000000009
#define eps 0.00000001
using namespace std; /*
dp[i]表示元素a[i]之前的最大comfort
*/
int dp[MAXN], l[MAXN], r[MAXN], a[MAXN], n;
bool been[MAXN];
int main()
{
scanf("%d", &n);
memset(l, INF, sizeof(l));
memset(r, -INF, sizeof(r));
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
l[a[i]] = min(i, l[a[i]]);
r[a[i]] = max(i, r[a[i]]);
}
for (int i = ; i <= n; i++)
dp[i] = -INF;
dp[] = ;
for (int i = ; i < n; i++)
if (dp[i] != -INF)
{
dp[i + ] = max(dp[i + ], dp[i]);
int L = i, R = i, sum = ;
memset(been, false, sizeof(been));
for (int j = i; j <= R; ++j)
{
L = min(L, l[a[j]]);
R = max(R, r[a[j]]);
if (!been[a[j]])
{
sum ^= a[j];
been[a[j]] = true;
}
}
if (L == i)
dp[R + ] = max(dp[R + ], dp[i] + sum);
}
printf("%d\n", dp[n]);
return ;
}
C. Vladik and Memorable Trip DP的更多相关文章
- CodeForces - 811C Vladik and Memorable Trip(dp)
C. Vladik and Memorable Trip time limit per test 2 seconds memory limit per test 256 megabytes input ...
- C. Vladik and Memorable Trip 解析(思維、DP)
Codeforce 811 C. Vladik and Memorable Trip 解析(思維.DP) 今天我們來看看CF811C 題目連結 題目 給你一個數列,一個區段的數列的值是區段內所有相異數 ...
- 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 ...
- CodeForce-811C Vladik and Memorable Trip(动态规划)
Vladik and Memorable Trip CodeForces - 811C 有一个长度为 n 的数列,其中第 i 项为 ai. 现在需要你从这个数列中选出一些互不相交的区间,并且保证整个数 ...
- Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)
<题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...
- 【dp】codeforces C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...
- codeforces 811 C. Vladik and Memorable Trip(dp)
题目链接:http://codeforces.com/contest/811/problem/C 题意:给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...
- CodeForces 811C Vladik and Memorable Trip
$dp$. 记录$dp[i]$表示以位置$i$为结尾的最大值. 枚举最后一段是哪一段,假设为$[j,i]$,那么可以用$max(dp[1]...dp[j-1]) + val[j][i]$去更新$dp[ ...
- CF811C Vladik and Memorable Trip
思路: 令dp[i]表示前i个的最大舒适度.则如果区间[j, i](1 < j <= i)满足条件,有如下转移:dp[i] = max(dp[i], dp[j - 1] + cur).其中 ...
随机推荐
- sql数据库CHECKDB时报x个分配错误和x个一致性错误
--1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...
- 详细解析Linux scp命令的应用(转载)
转自:http://os.51cto.com/art/201003/187301.htm Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程 ...
- Filter,Interceptor和Aspect
过滤器使用的主要是反射 :拦截器使用的主要是回调 :AOP使用的主要是动态代理. 一个请求过来 ,先进行过滤器处理,看程序是否受理该请求.过滤器放过后, 程序中的拦截器进行处理,处理完后进入被AOP动 ...
- github 用户不被识别问题
期末考完,继续开发. 用过的都知道,直接用的话贡献者上面显示不出自己. 查一下就知道是因为github的识别是靠邮箱设置的. 但是如果频繁创建新仓库,容易忘记设定用户名和邮箱. 突发奇想,发现 ...
- 面试题:InnoDB中一棵B+树能存多少行数据?
阅读本文大概需要 5 分钟. 作者:李平 | 来源:个人博客 一.InnoDB 一棵 B+ 树可以存放多少行数据? InnoDB 一棵 B+ 树可以存放多少行数据? 这个问题的简单回答是:约 2 千万 ...
- Spring实例化bean之后的处理, 关于BeanPostProcessor接口的使用
业务需求:缓存页面,展示需要缓存的所有对象,每类对象在字典表中有编码对应,点击某个对象可以缓存某类对象,每类对象都有自己的缓存runner(弱弱的说一句,本人看到这里的第一反应就是if-else,捂脸 ...
- Git 标记操作
.推送标签: git push origin 标签名 .删除本地标签: git tag -d 标签名 .删除远程标签: git push origin :refs/tags/标签名 例:git pus ...
- c# regex Match Matches MatchCollection 用法
string text = "1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080"; Reg ...
- scala 变量定义,基本操作符
scala是jvm语言,它将面向对象与函数风格结合的很好,它可以访问任何java类库并很好的结合使用. 它可以使程序更简单,同时可利用并发的威力. scala基本语法: package com.tes ...
- Unity引擎 UGUI
Unity UGUI讲解 1.导入UI图片资源 2.设置参数: TextureType(纹理类型) 精灵 2D and UI SpriteMode(精灵模式) Single(单) multiple( ...