B. The Bakery

http://codeforces.com/contest/833/problem/B

题意:

  将一个长度为n的序列分成k份,每份的cost为不同的数的个数,求最大cost的和。1≤n≤35000,1≤k≤50

分析:

  dp[i][j]表示前i个数,分了j份。dp[i][k]=dp[j][k-1]+cost(j+1,i);cost(j+1,i)为这一段中不同数的个数。

  然后考虑如何优化。发现每次增加一个位置,pre[i]~i-1区间的每个转移的位置的cost+1。然后每次转移是在上一个dp数组中取最大值,于是线段树维护。

代码:

 /*
* @Author: mjt
* @Date: 2018-10-15 14:52:11
* @Last Modified by: mjt
* @Last Modified time: 2018-10-15 15:27:53
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int dp[N][], pre[N], last[N], a[N];
int n, k; #define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct SegmentTree {
int mx[N << ], tag[N << ], Cur;
inline void pushup(int rt) { mx[rt] = max(mx[rt << ], mx[rt << | ]); }
inline void pushdown(int rt) {
if (tag[rt]) {
tag[rt << ] += tag[rt]; mx[rt << ] += tag[rt];
tag[rt << | ] += tag[rt]; mx[rt << | ] += tag[rt];
tag[rt] = ;
}
}
void build(int l,int r,int rt) {
tag[rt] = ;
if (l == r) {
mx[rt] = dp[l][Cur]; return ;
}
int mid = (l + r) >> ;
build(lson); build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
mx[rt] ++; tag[rt] ++;
return ;
}
pushdown(rt);
int mid = (l + r) >> ;
if (L <= mid) update(lson, L, R);
if (R > mid) update(rson, L, R);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
return mx[rt];
}
pushdown(rt);
int mid = (l + r) >> , res = ;
if (L <= mid) res = max(res, query(lson, L, R));
if (R > mid) res = max(res, query(rson, L, R));
return res;
}
}T; void solve(int now) {
T.Cur = now - ; T.build(Root);
for (int i=now; i<=n; ++i) {
T.update(Root, pre[i], i - ); // 线段树维护的是dp[j][now-1]+cost(j+1,i)的值,所以pre[i]~i-1这个区间加1!!!
dp[i][now] = T.query(Root, , i - );
}
} int main() {
n = read(), k = read();
for (int cnt=,i=; i<=n; ++i) {
a[i] = read();
pre[i] = last[a[i]]; last[a[i]] = i;
if (pre[i] == ) cnt ++;
dp[i][] = cnt;
}
for (int i=; i<=k; ++i) solve(i);
cout << dp[n][k];
return ;
}

CF 833 B. The Bakery的更多相关文章

  1. 【题解】CF#833 B-The Bakery

    一个非常明显的 \(nk\) dp 状态 \(f[i][k]\) 表示以 \(i\) 为第 \(k\) 段的最后一个元素时所能获得的最大代价.转移的时候枚举上一段的最后一个元素 \(j\)更新状态即可 ...

  2. cf 833 A 数论

    A. The Meaningless Game time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. Codeforeces 707B Bakery(BFS)

    B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. CodeForces–833B--The Bakery(线段树&&DP)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  5. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  8. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  9. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

随机推荐

  1. git用户限制ssh登录服务器

    服务器额外的防范措施: 搭建git服务器后通常会建立一个git账户,其它人共用这个账户来克隆或推送数据到git仓库中,通常也只需要这个功能,但是如果不加限制,那么其它人可以通过这个git账户登录到主机 ...

  2. 博客存档TensorFlow入门一 1.4编程练习

        import tensorflow as tf import numpy import matplotlib.pyplot as plt #from sklearn.model_selecti ...

  3. 【luogu P1156 垃圾陷阱】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1156 设\(dp[i][j]\)表示前i堆到达高度j时的所活最长时间 那么一旦到当前状态能到达满足的时间和高 ...

  4. 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)

    1. Concurrency的作用 场景有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据)正确的该User的年龄是25, ...

  5. 生成二维码的 jQuery 插件:jquery.qrcode.js的中文乱码问题

    在使用jquery.qrcode.js这个插件生成二维码的时候发现并不能识别中文. 原因在于:jquery-qrcode是采用charCodeAt()方式进行编码转 换的. 而这个方法默认会获取它的U ...

  6. ubuntu下USB口插入USB转TTL查看串口号

    首先先要获取权限 sudo su 然后 cd /devls ls可以列出所有的串口号(确保此时USB转TTL已经插在电脑上了) 然后拔掉USB转TTL 在ls一下列出所有的串口设备 对比可以发现,插上 ...

  7. 一个loser的忏悔

    一直认为自己是世界的主角,从小意气用事,耽误了学业,现在才发现了自己的爱好,于是开始努力进阶. 愿不辜负自己的努力! 高中大学青葱旺盛的美好时期全部用在了感叹人生上,只能在30岁的年纪重新扛起学业,活 ...

  8. Java中 方法的多态 简析图

    代码如下: public class Client{    public static void main(String[] args){        Person p = new Person() ...

  9. Linux基础-4.正文处理命令及tar命令

    1.使用cat命令进行文件的纵向合并 1)掌握使用cat命令的纵向合并 a)例如:使用cat命令将test1.file1.txt和file2这三个文件纵向合并为file文件的命令为: cat test ...

  10. 记如何用树莓派3开一个无线AP

    开热点 忘掉自己手动配置吧 create_ap git clone git@github.com:oblique/create_ap.git cd create_ap sudo make instal ...