CF 833 B. The Bakery
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的更多相关文章
- 【题解】CF#833 B-The Bakery
一个非常明显的 \(nk\) dp 状态 \(f[i][k]\) 表示以 \(i\) 为第 \(k\) 段的最后一个元素时所能获得的最大代价.转移的时候枚举上一段的最后一个元素 \(j\)更新状态即可 ...
- cf 833 A 数论
A. The Meaningless Game time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforeces 707B Bakery(BFS)
B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CodeForces–833B--The Bakery(线段树&&DP)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
随机推荐
- 协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)
网络层(network layer)是实现互联网的最重要的一层.正是在网络层面上,各个局域网根据IP协议相互连接,最终构成覆盖全球的Internet.更高层的协议,无论是TCP还是UDP,必须通过网络 ...
- BZOJ3208:花神的秒题计划Ⅰ(记忆化搜索DP)
Description 背景[backboard]: Memphis等一群蒟蒻出题中,花神凑过来秒题…… 描述[discribe]: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区 ...
- gluoncv 用已经训练好的模型参数,检测物体
当然这个模型参数,最好用自己的,否则不够精确,我自己的还没训练完. from matplotlib import pyplot as plt import gluoncv from gluoncv i ...
- 【[SDOI2014]数表】
求 \[\sum_{i=1}^N\sum_{j=1}^Mσ(gcd(i,j))[σ(gcd(i,j))<=a]\] \(σ\)表示约数和函数 感觉非常难求的样子 先把套路搞出来 \[f(n)=\ ...
- PHP整数转小数
sprintf('%.2f',$memrow['yjrebate1']+$memrow['yjrebate2'])
- SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成ResultMap
其实我一点都不想用mybatis,好多地方得自己写,比如这里. 使用mybatis要写大量的xml,烦的一批.最烦人的莫过于写各种resultmap,就是数据库字段和实体属性做映射.我认为这里应该是m ...
- 自定义组件---图片和文字实现ImageButton效果
1.效果图 2.自定义代码: <span style="font-family:Comic Sans MS;font-size:14px;">public class ...
- HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) ...
- Python中获取异常(try Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置. 这里获取异常(Exception)信息采用try...except...程序结构.如下所示: try: ... exce ...
- html标签种类
标签 描述 <!--...--> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义锚. <abbr> 定义缩写. <acronym& ...