维护前面的position+主席树 Codeforces Round #406 (Div. 2) E
http://codeforces.com/contest/787/problem/E
题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色。另k=1,2,3...n,问在每一种情况下,输出能划分出的最小的段落数。
例如:
5
1 3 4 3 3
- [1], [3], [4], [3, 3]
- [1], [3, 4, 3, 3]
- [1, 3, 4, 3, 3]
- [1, 3, 4, 3, 3]
- [1, 3, 4, 3, 3]
思路:太久没写过主席树了,有点傻了
先说这题的弱化版,只对于一个k=x的情况,统计能划分成几段
其实这个问题,我们就只需要暴力一遍,定义lb=1,然后一直往后面暴力,for(rb; rb <=n; rb++)然后加入节点就用segment tree来维护。如果color的数目达到了k,我们就移动lb,让lb一直移动到等于rb,并在移动的同时删除线段树上的信息即可。所以复杂度是O(n*logn)
那么对于每一个k,我们可以利用上面的思路来解决这个问题
下面这一段来自这个人的:http://kugwzk.info/index.php/archives/2296
然后我这里在主席树上面找的方法就是找还需要剩下多少颜色
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int n;
struct Tree{
int lb, rb, val;
}tree[maxn << ];
int pre[maxn], a[maxn], head[maxn];
int k, tot; int update(int pos, int l, int r, int o, int cost){
int k = ++tot;
tree[k] = tree[o];
tree[k].val += cost;
if (l == pos && r == pos) return k;
int mid = (l + r) / ;
if (pos <= mid) tree[k].lb = update(pos, l, mid, tree[o].lb, cost);
if (pos > mid) tree[k].rb = update(pos, mid + , r, tree[o].rb, cost);
return k;
} int query(int l, int r, int o, int cost){
if (l == r) return l;
int mid = (l + r) / , lb = tree[o].lb, rb = tree[o].rb;
if (tree[lb].val >= cost) return query(l, mid, lb, cost);
return query(mid + , r, rb, cost - tree[lb].val);
} int main(){
cin >> n;
for (int i = ; i <= n; i++){
scanf("%d", a + i);
head[i] = update(i, , n, head[i - ], );
if (pre[a[i]]) head[i] = update(pre[a[i]], , n, head[i], -);
pre[a[i]] = i;
}
for (int i = ; i <= n; i++){
int ans = , pos = n;
while (true){///因为要找i个不一样的颜色的,就要找个数为n-i个的
int need = tree[head[pos]].val - i;
if (need <= ){ans++; break;}
pos = query(, n, head[pos], need);
ans++;
}
printf("%d ", ans);
}
cout << endl; return ;
}
维护前面的position+主席树 Codeforces Round #406 (Div. 2) E的更多相关文章
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces #Round 406(Div.2)
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------- 大家好,我是一个假人.在学习O ...
- 【Codeforces Round #406 (Div. 2)】题解
The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D
http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
随机推荐
- Python学习之路8 - 内置方法
abs(-230) #取绝对值 all([0,1,-5]) #如果参数里面的所有值都为真就返回真,否则返回假 any([0,1,-5]) #如果参数里面有一个值为真则返回真,否则返回假 ascii([ ...
- 从一个app开始学iOS
在大学上了4年学,老师一直给灌输的思想就是,从细微处着手最后看到整体.举个网站的例子.第一个学期老师安排一门课java语言,期末考试就是考试java语言的知识.第二学期java web,第一次课配置j ...
- Ubuntu环境下No module named '_tkinter'错误的解决
在Ubuntu环境下运行下面代码: import matplotlib as plt 出现以下错误: No module named '_tkinter' 解决方法: sudo apt-get ins ...
- 机器学习实战第二章----KNN
tile的使用方法 tile(A,n)的功能是把A数组重复n次(可以在列方向,也可以在行方向) argsort()函数 argsort()函数返回的是数组中值从大到小的索引值 dict.get()函数 ...
- Storm事务Topology的接口介绍
ITransactionalSpout 基本事务Topology的Spout接口,内含两部分接口:协调Spout接口以及消息发送Blot接口. TransactionalSpoutBatchExe ...
- 小程序 坐标算距离 (copy)
var EARTH_RADIUS = 6378137.0; //单位M var PI = Math.PI; function getRad(d){ retu ...
- 条形码生成库 BarcodeLib
官方介绍 在ASP.NET,Windows,Reporting Service,Crystal Reports 和 RDLC Reports应用程序中轻松生成条形码 生成准确的条形码图像,并可以保存为 ...
- PHP与类有关的几个魔术方法
与类有关的其他魔术方法 序列化与反序列化技术 含义: 序列化: 就是将一个变量所代表的“内存”数据,转换为“字符串”形式并持久保存在硬盘上的一种做法. 反序列化: 就是将序列化之后保存在硬盘上的“字符 ...
- 第129天:node.js安装方法
node.js安装方法 第一步:双击node.js安装包开始安装,注意64位和32位,按照自己的进行安装 第二步:在安装过程中一直选择next,在选择安装目录时,大多数默认安装在C盘,我安装在了D盘, ...
- Java Machine Learning Tools & Libraries--转载
原文地址:http://www.demnag.com/b/java-machine-learning-tools-libraries-cm570/?ref=dzone This is a list o ...