维护前面的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 Daily软件Alpha版本——基于spec评论
题目要求:每个小组评论其他小组Alpha发布作品的软件功能说明书.要求和提交在[https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/122 ...
- pspo过程文档
项目计划总结: 日期/任务 听课 编写程序 阅读相关书籍 日总计 周一 110 60 ...
- 图论---POJ 3660 floyd 算法(模板题)
是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...
- 1029 C语言文法定义
program à external_declaration | program external_declaration <源程序> -> <外部声明> | < ...
- 内存测试——Android Studio中对应进程的Heap
通过Android Studio的Heap查看该程序的目前占用内存大小,多次进出界面,观察内存内存大小的变化.用Heap监测应用进程使用内存情况的步骤如下: 1. 启动Android Studio—& ...
- Centos7安装完毕后联网-设置ip地址(VMware虚拟机)
VMware虚拟机中安装了Centos7,为了让Centos能够访问外网及设置固定的ip地址以方便本地通过SSH访问Centos,做以下几步.本文来自osfipin note. 1.确认虚拟机网络链接 ...
- Java NIO中的Buffer
简介 Buffer缓冲区,首先要弄明白的是,缓冲区是怎样一个概念.它其实是缓存的一种,我们常说的缓存,包括保存在硬盘上的浏览器缓存,保存在内存中的缓存(比如Redis.memcached).Buffe ...
- [十七]SpringBoot 之 使用自定义的properties
Springboot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 如果继续在application.properties中添加 如: ...
- DNA Sequence POJ - 2778 (ac自动机 + 快速幂)
题意: 给出患病的DNA序列,问序列长度为n的,且不包含患病的DNA序列有多少种 解析: 以给出的患病DNA序列建trie树 患病结点要用flag标记 对于长度为n的序列 位置i有四种 情况A C ...
- The Necklace UVA - 10054(欧拉回路)
题目分析:1.无向图欧拉回路是否连通2.所有点的度为偶数.并查集+degree 这题题目保证了是联通的 所以就不用判断是否联通了 #include <iostream> #include ...