日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 786C——二分+模拟?

發(fā)布時(shí)間:2023/11/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 786C——二分+模拟? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【題目描述】

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.

Input

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.

Output

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.

Examples
Input

5 1 3 4 3 3

Output

4 2 1 1 1

Input

8 1 5 7 8 1 7 6 1

Output

8 4 3 2 1 1 1 1

【題目分析】
這道題放在線段樹(shù)里面我實(shí)在沒(méi)有什么想法,覺(jué)得就暴力一下不可以嗎?可是估計(jì)了一下時(shí)間應(yīng)該會(huì)超時(shí),在網(wǎng)上看到一種想法,就是二分暴力,如果區(qū)間兩端答案一樣那么說(shuō)明中間的所有的答案都是一樣的,因?yàn)榇鸢笐?yīng)該是非線性遞減的。
【AC代碼】

#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #include<climits> #include<queue> #include<vector> #include<set> #include<map> using namespace std;typedef long long ll; const int INF=0x3f3f3f3f; const int MAXN=1e5+5; int a[MAXN]; int vis[MAXN]; int ans[MAXN]; int n;void Read() {scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);} }int GetAns(int k) //模擬 {memset(vis,-1,sizeof(vis));int cnt=0,ret=1;for(int i=1;i<=n;i++){if(vis[a[i]]==ret) continue;cnt++;if(cnt>k){ret++;cnt=1;}vis[a[i]]=ret;}return ret; }void Solve(int l,int r) //二分 {if(l>r) return;int ansl=GetAns(l);int ansr=GetAns(r);if(ansl==ansr){for(int i=l;i<=r;i++){ans[i]=ansl;}return;}ans[l]=ansl; ans[r]=ansr;int mid=(l+r)>>1;Solve(l+1,mid); Solve(mid+1,r-1); }void Print() {for(int i=1;i<=n;i++){printf("%d ",ans[i]);} }int main() {Read();Solve(1,n);Print();return 0; }

總結(jié)

以上是生活随笔為你收集整理的CodeForces - 786C——二分+模拟?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。