http://codeforces.com/contest/787/problem/E

题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色。另k=1,2,3...n,问在每一种情况下,输出能划分出的最小的段落数。

例如:

5
1 3 4 3 3
ans = 4, 2, 1, 1, 1
  1. [1], [3], [4], [3, 3]
  2. [1], [3, 4, 3, 3]
  3. [1, 3, 4, 3, 3]
  4. [1, 3, 4, 3, 3]
  5. [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

首先不管怎么说,肯定是能尽量拿尽量拿,保证一个队尽可能的长。然后我们在枚举k的时候,其实已经有了一个O(nlogn)的复杂度了:因为n+n/2+n/3+....n/n=nlogn。。。所以我们必须要在logn的时间内找到对于一个位置i而言,最大的一个j,满足i到j的颜色数不超过k。那也就需要知道[i,j]这样一段区间有多少个不同的颜色。
可以主席树维护一下这个东西,我们从位置1开始维护,用一个pre记录下来每种颜色的上一个位置在哪里。第i棵主席树表示的含义就是从1-i这样一段区间内的不同的颜色数目。也就是每个节点都存一下他所代表的区间内的颜色个数。因为我们记录下来了之前那种颜色的位置,所以新加入i位置的时候,在那个位置上减去1,在i位置上加上1.这样每次修改O(logn)个节点。从而满足统计出来1-i的颜色个数。

然后我这里在主席树上面找的方法就是找还需要剩下多少颜色

//看看会不会爆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的更多相关文章

  1. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  2. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  3. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  4. Codeforces #Round 406(Div.2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------- 大家好,我是一个假人.在学习O ...

  5. 【Codeforces Round #406 (Div. 2)】题解

    The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...

  6. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  7. 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 ...

  8. 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D

    http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...

  9. 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 ...

随机推荐

  1. 第七次作业PSP

    psp 进度条 代码累积折线图 博文累积折线图 psp饼状图

  2. mac安装php分词工具xunsearch出现找不到bio.h的解决办法

    下载xunsearch后安装出现如下错误,在xunsearch官方论坛未找到答案,此方案不仅用于参考解决安装xunsearch,其它编辑安装出现的问题同样可以参考 -n Checking scws . ...

  3. android入门 — Service

    Service完全在后台运行,没有用户界面.使用的时候先创建Service子类,然后在AndroidManifest.xml中进行注册,同时可以通过<intent-filter.../>进 ...

  4. Python学习笔记(二)--变量和数据类型

    python中的数据类型 python中什么是变量 python中定义字符串 raw字符串与Unicode字符串 python中的整数和浮点数 python中的bool类型 --- python中的数 ...

  5. lintcode-442-实现 Trie

    442-实现 Trie 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 注意事项 你可以假设所有的输入都是小写字母a-z. 样例 insert(&qu ...

  6. CentOS7实现RabbitMQ高可用集群

    CentOS安装RabbitMQ集群 1.有3台已安装RabbitMQ的机器 192.168.38.133 rabbitmq1 192.168.38.137 rabbitmq2 192.168.38. ...

  7. IP ,路由

    ifconfig 命令       ip信息   enp0s3: flags=4163<UP(已经启用),BROADCAST(支持广播),RUNNING,MULTICAST(支持多播)> ...

  8. java分页算法

    int totalPageNum = (totalRecord  +  pageSize  - 1) / pageSize;

  9. i18n实现前端国际化(实例)

    在今日的需求中需要利用 i18n 这个框架来实现前端的国家化操作,下图是实现效果: 点击选择框实现网页上语言的切换: 下面开始实现过程: 所需工具:    - jquery-3.3.1.js 下载地址 ...

  10. Spring boot整合shiro框架

    ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...