Codeforces Round #406 (Div. 1) C. Till I Collapse(可持久化線段樹)
Rick and Morty want to find MR. PBH and they can't do it alone. So they need of Mr. Meeseeks. They Have generated n Mr. Meeseeks, standing in a line numbered from 1 to n. Each of them has his own color. i-th Mr. Meeseeks' color is ai.
Rick and Morty are gathering their army and they want to divide Mr. Meeseeks into some squads. They don't want their squads to be too colorful, so each squad should have Mr. Meeseeks of at most k different colors. Also each squad should be a continuous subarray of Mr. Meeseeks in the line. Meaning that for each 1 ≤ i ≤ e ≤ j ≤ n, if Mr. Meeseeks number i and Mr. Meeseeks number j are in the same squad then Mr. Meeseeks number e should be in that same squad.
Also, each squad needs its own presidio, and building a presidio needs money, so they want the total number of squads to be minimized.
Rick and Morty haven't finalized the exact value of k, so in order to choose it, for each k between 1 and n (inclusive) need to know the minimum number of presidios needed.
The first line of input contains a single integer n (1 ≤ n ≤ 105) — number of Mr. Meeseeks.
The second line contains n integers a1, a2, ..., an separated by spaces (1 ≤ ai ≤ n) — colors of Mr. Meeseeks in order they standing in a line.
In the first and only line of input print n integers separated by spaces. i-th integer should be the minimum number of presidios needed if the value of k is i.
分析:列舉k後,每次可以貪心的O(n)掃一遍得到答案,每次貪心的過程可以用二分加速,但是這樣下來每趟複雜度是logn^2的,然後我們考慮主席樹,每次貪心相當於在一個區間中找到包含k個不同的數的最大的一段字首,我們可以用主席樹求區間不同數的方法把它轉化為求區間的第k大樹,這樣每趟下來複雜度是logn,總複雜度nlogn^2.
#include <bits/stdc++.h>
#define N 100005
#define INF 2147483647
using namespace std;
int n,cnt,a[N],rt[N],Pre[N],ans[N];
struct Node
{
int l,r,ls,rs,sum;
}tr[N*40];
void Build(int &node,int l,int r)
{
node = ++cnt;
tr[node].l = l;
tr[node].r = r;
if(l == r) return;
int mid = (l + r) >> 1;
Build(tr[node].ls,l,mid);
Build(tr[node].rs,mid+1,r);
}
void Insert(int pre,int &node,int pos,int x)
{
node = ++cnt;
tr[node] = tr[pre];
tr[node].sum += x;
if(tr[node].l == tr[node].r) return;
int mid = (tr[node].l + tr[node].r)>>1;
if(pos <= mid) Insert(tr[pre].ls,tr[node].ls,pos,x);
else Insert(tr[pre].rs,tr[node].rs,pos,x);
}
int Find(int node,int kth)
{
if(tr[node].l == tr[node].r) return tr[node].l;
if(kth <= tr[tr[node].ls].sum) return Find(tr[node].ls,kth);
else return Find(tr[node].rs,kth-tr[tr[node].ls].sum);
}
int get(int x,int k)
{
int kth = tr[rt[x]].sum-k;
if(kth <= 0) return 1;
return Find(rt[x],kth) + 1;
}
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
Build(rt[0],1,n);
for(int i = 1;i <= n;i++)
{
if(Pre[a[i]])
{
Insert(rt[i-1],rt[i],Pre[a[i]],-1);
Insert(rt[i],rt[i],i,1);
}
else Insert(rt[i-1],rt[i],i,1);
Pre[a[i]] = i;
}
for(int k = 1;k <= n;k++)
{
int r = n,l;
while(r)
{
ans[k]++;
l = get(r,k);
r = l-1;
}
cout<<ans[k]<<" ";
}
}
相關文章
- Codeforces Round #406 (Div. 1) B. Legacy(線段樹優化建圖)優化
- Codeforces Round #373 (Div. 1) C. Sasha and Array 線段樹
- Codeforces Round #675 (Div. 2) 1442 F - Boring Queries 可持久化線段樹維護 區間乘法持久化
- Codeforces Round #228 (Div. 1) C. Fox and Card GameGAM
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- Codeforces Round #446 (Div. 2) C. PrideIDE
- Codeforces Round #242 (Div. 2) C. Magic FormulasORM
- 「學習筆記」可持久化線段樹筆記持久化
- C. Lose it!(思維)Codeforces Round #565 (Div. 3)
- Codeforces Round #215 (Div. 2) C. Sereja and AlgorithmGo
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- 【資料結構】可持久化線段樹初步資料結構持久化
- 演算法隨筆——主席樹(可持久化線段樹)演算法持久化
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Arraydev
- Codeforces Round 934 (Div. 1)
- Codeforces C. Colored Rooks 構造 (Codeforces Round #518 (Div. 2) )
- Codeforces Round #688 (Div. 2) C. Triangles(思維,數學)
- C. Dominant Piranha(思維) Codeforces Round #677 (Div. 3)NaN
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水題
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二進位制]
- Codeforces Beta Round #76 (Div. 1 Only) A. Frames
- Codeforces Round 943 (Div. 3)(C-G1)
- Codeforces Round #207 (Div. 2)C. Knight Tournament(SET也可以搞定)
- Educational Codeforces Round 165 (Rated for Div. 2) C. Minimizing the Sum題解
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #690 (Div. 3)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)