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 ...
随机推荐
- 51nod 1967路径定向(dfs、欧拉回路)
1967 路径定向 基准时间限制:1.2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 给出一个有向图,要求给每条边重定向,使得定向后出度等于入度的点最多,输出答案和任意一种方案 ...
- CSS(层叠样式表)基础知识
CSS 指层叠样式表 (Cascading Style Sheets).样式定义怎样显示 HTML 元素.它通常存储在样式表中,把样式加入到 HTML 4.0 中,解决内容与表现分离的问题. 当同一 ...
- pip-修改为国内镜像源
pip 常用命令 pip install ./downloads/SomePackage-1.0.4.tar.gz pip install http://my.package.repo/SomePac ...
- BZOJ1485:[HNOI2009]有趣的数列(卡特兰数)
Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...
- 「CF1025D Recovering BST」
题目 郑州讲过的题了 发现这是一个二叉搜索树,给出的还是中序遍历,我们很自然的想到我们需要可以用一个\(f[i][j][k](k\in[i,j])\)来表示区间\([i,j]\)能不能形成以\(k\) ...
- 好用的纯CSS加载动画-spinkit
首先放一个css spinkit <style> .loaders{ width: 100%; height: 100%; padding: 100px; box-sizing: bor ...
- [转]MVP+WCF+三层结构搭建项目框架
最近,我一直在重构之前做的一个项目,在这个过程中感慨万千.原先的项目是一个运用了WCF的C/S系统,在客户端运用了MVC模式,但MVC的View.Model耦合以及WCF端分布式欠佳等问题让我有了重构 ...
- PAT——1021. 个位数统计
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...
- nginx 反向代理 proxy_pass 及对比nginx与haproxy反向代理服务器功能、性能的优劣
1.使用 proxy 去请求另一个域名下的资源,如果跨域资源也部署在同一台机器上,我们甚至可以 proxy 到 127.0.0.1,比如: location /api { proxy_pass htt ...
- 【星云测试】Wings-让单元测试智能全自动生成
Wings-让单元测试智能全自动生成 前言 单元测试是保证软件质量非常有效的手段,无论是从测试理论早期介入测试的理念来看或是从单元测试不受UI影响可以高速批量验证的特性,所以业界所倡导的测试驱动开发, ...