维护前面的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 ...
随机推荐
- 软件工程第七周psp
1.PSP表格 类别 任务 开始时间 结束时间 中断时间 delta时间 立会 汇报昨天的成绩,分配任务,部署计划 10月27日18:00 10月27日18:36 0 36分钟 准备工作 查阅有关资料 ...
- iframe高度自适应的6个方法
原文链接:http://caibaojian.com/iframe-adjust-content-height.html JS自适应高度,其实就是设置iframe的高度,使其等于内嵌网页的高度,从而看 ...
- vs快捷键代码格式化或代码对齐名字
开发人员,换个电脑后环境要重装,vs的环境也需要重新设置. 快捷键需要重新设置,插件也需要重装,在这里备注下,换个环境就可以直接用了. 由于vs不同版本,代码对齐或者代码格式化的快捷键都不一样,所以导 ...
- WebService(三)
JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...
- 【leetcode】62.63 Unique Paths
62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...
- 更新user的方法
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserChangeForm ...
- Hibernate 中 load() 方法导致的 noSession 异常
之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...
- android Eclipse there no select
点mainactivity类 右键 run as 进行 配置 就可运行
- 51nod-1220-约数之和
题意 求 \[ \sum _{i=1}^n\sum _{j=1}^nd(ij) \\ d(x)=\sum _{e|x}e \] \(n\le 10^9\) . 分析 没有推出来.这题有几个要点要学习. ...
- 堆模板(pascal)洛谷P3378
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...