题目链接:http://codeforces.com/contest/834/problem/D

题意:给定一个长度为n的序列和一个k,现在让你把这个序列分成刚好k段,并且k段的贡献之和最大。对于每一段的贡献为该段有多少个不同的数字。

思路:考虑dp, dp[k][i]表示前i个数切k段的答案,那么dp[k][i]=max(dp[k-1][j]+color(j+1,i)) [1<=j<i], [color(l,r)表示区间[l,r]有多少个不同的数字] ,由于第k行的dp值只会影响到k+1行的dp值,所以我们可以把k这一维忽略掉。考虑转移dp[k][i+1],新增加的i+1这个点会影响到[pre[a[i]]+1,i]这段区间+1。

ftiasch的题解

我们在建线段树的时候,树上的结点(结点表示的区间为[l,r])维护的是g[k](即dp[k-1][r-1]),然后就是区间加和区间最大值了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <bitset>
using namespace std;
typedef long long LL;
const int MAXN = + ; int n,k,pre[MAXN],dp[MAXN];
struct Color{
int val,l,r;
}c[MAXN]; //Segment Tree
#define L(x)(x<<1)
#define R(x)(x<<1|1)
struct Node{
int l,r,Lazy,maxval;
Node(int _l=,int _r=,double _val=){
l=_l; r=_r; maxval=_val;
}
}Seg[MAXN*];
void pushUp(int k){
Seg[k].maxval=max(Seg[L(k)].maxval,Seg[R(k)].maxval);
}
void pushDown(int k){
if(Seg[k].Lazy){
Seg[L(k)].Lazy+=Seg[k].Lazy;
Seg[L(k)].maxval+=Seg[k].Lazy;
Seg[R(k)].Lazy+=Seg[k].Lazy;
Seg[R(k)].maxval+=Seg[k].Lazy;
Seg[k].Lazy=;
}
}
void Build(int st,int ed,int k){
Seg[k].l=st; Seg[k].r=ed; Seg[k].Lazy=;
if(st==ed){
Seg[k].maxval=dp[st-];
return;
}
int mid=(st+ed)>>;
Build(st,mid,L(k)); Build(mid+,ed,R(k));
pushUp(k);
}
void Modify(int st,int ed,int k,int val){
if(Seg[k].l==st&&Seg[k].r==ed){
Seg[k].maxval+=val;
Seg[k].Lazy+=val;
return;
}
pushDown(k);
if(Seg[L(k)].r>=ed){
Modify(st,ed,L(k),val);
}else if(Seg[R(k)].l<=st){
Modify(st,ed,R(k),val);
}else{
Modify(st,Seg[L(k)].r,L(k),val); Modify(Seg[R(k)].l,ed,R(k),val);
}
pushUp(k);
}
int Query(int st,int ed,int k){
if(Seg[k].l==st&&Seg[k].r==ed){
return Seg[k].maxval;
}
pushDown(k);
int res;
if(Seg[L(k)].r>=ed){
res=Query(st,ed,L(k));
}else if(Seg[R(k)].l<=st){
res=Query(st,ed,R(k));
}else{
res=max(Query(st,Seg[L(k)].r,L(k)),Query(Seg[R(k)].l,ed,R(k)));
}
pushUp(k);
return res;
}
int main(){
#ifdef kirito
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d",&n,&k)) {
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++){
scanf("%d",&c[i].val);
c[i].r=i; c[i].l=pre[c[i].val]+; pre[c[i].val]=i;
}
for(int i=;i<=n;i++){
dp[i]=dp[i-]+(c[i].l==?:);
}
for(int j=;j<=k;j++){
Build(,n,);
for(int i=;i<=n;i++){
Modify(c[i].l,c[i].r,,);
dp[i]=Query(,i,);
}
}
printf("%d\n",dp[n]);
}
return ;
}

Codeforces Round #426 (Div. 2) - D的更多相关文章

  1. CodeForces 834C - The Meaningless Game | Codeforces Round #426 (Div. 2)

    /* CodeForces 834C - The Meaningless Game [ 分析,数学 ] | Codeforces Round #426 (Div. 2) 题意: 一对数字 a,b 能不 ...

  2. Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】

    A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  3. Codeforces Round #426 (Div. 2)

    http://codeforces.com/contest/834 A. The Useless Toy 题意: <,>,^,v这4个箭头符号,每一个都可以通过其他及其本身逆时针或者顺时针 ...

  4. Codeforces Round #426 (Div. 2) C. The Meaningless Game

    C. The Meaningless Game 题意: 两个人刚刚开始游戏的时候的分数, 都是一分, 然后随机一个人的分数扩大k倍,另一个扩大k的平方倍, 问给你一组最后得分,问能不能通过游戏得到这样 ...

  5. Codeforces Round #426 (Div. 2)A B C题+赛后小结

    最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...

  6. Codeforces Round #426 (Div. 2) A,B,C

    A. The Useless Toy 题目链接:http://codeforces.com/contest/834/problem/A 思路: 水题 实现代码: #include<bits/st ...

  7. Codeforces Round #426 (Div. 2)A题&&B题&&C题

    A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...

  8. 【Codeforces Round #426 (Div. 2) A】The Useless Toy

    [Link]:http://codeforces.com/contest/834/problem/A [Description] [Solution] 开个大小为4的常量字符数组; +n然后余4,-n ...

  9. 【Codeforces Round #426 (Div. 2) B】The Festive Evening

    [Link]:http://codeforces.com/contest/834/problem/B [Description] [Solution] 模拟水题; 注意一个字母单个出现的时候,结束和开 ...

  10. 【Codeforces Round #426 (Div. 2) C】The Meaningless Game

    [Link]:http://codeforces.com/contest/834/problem/C [Description] 有一个两人游戏游戏; 游戏包括多轮,每一轮都有一个数字k,赢的人把自己 ...

随机推荐

  1. vue注

    1.vue-cli3创建运行项目: vue-cli3可视化创建项目: vue ui vue-cli3创建项目:vue create elementUI(elementUI为项目名称) vue-cli3 ...

  2. lr中用C语言比较两个字符串变量

    以下脚本,定义两个一样的字符数组,对比后,打印出result的值: Action() { int result; char string1[] = "We can see the strin ...

  3. 使用foreach进行批量更新

    public void addEmps(@Param("emps")List<Employee> emps); 2映射文件配置 <!-- 批量保存 --> ...

  4. Type.MakeGenericType 方法 (Type[]) 泛型反射

    替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型的 Type 对象. 命名空间:   System程序集:  mscorlib(mscorlib.dll 中) public ...

  5. leetcode 287寻找重复数

    这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: class Solution { public: int findDuplicate(vec ...

  6. python学习之requests基础

    学习地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#id2 使用requests发送网络请求 一.导入requ ...

  7. Unity3D 连接MySql

    MySql安装如下: https://www.cnblogs.com/dlvguo/p/9671832.html Unity连接MySql首先要在Unity的安装目录D:\Unity2017\Edit ...

  8. 2018.03.27 pandas duplicated 和 replace 使用

    #.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...

  9. es笔记---新建es索引

    es对索引的一堆操作都是用restful api去进行的,参数时一堆json,一年前边查边写搞过一次,这回搞迁移,发现es都到6.0版本了,也变化了很多,写个小笔记记录一下. 创建一个es索引很简单, ...

  10. django连接数据库的类型

    字段类型 django的models里面字段类型除了上面的常用的 models.CharField和models.IntegerField,还有更多的类型 1.models.AutoField 自增列 ...