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

【题目描述】

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 

【题目分析】
这道题放在线段树里面我实在没有什么想法,觉得就暴力一下不可以吗?可是估计了一下时间应该会超时,在网上看到一种想法,就是二分暴力,如果区间两端答案一样那么说明中间的所有的答案都是一样的,因为答案应该是非线性递减的。
【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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384044.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

迷宫求解(非递归)

上篇文章写出了利用函数形成栈桢的特性完成迷宫求解问题, 本篇文章我们自己手动维护一个栈, 其进行出栈, 入栈, 取栈顶元素, 来完成迷宫求解寻路的过程     思路和以前一样, 首先, 我们先定义一个栈, 对其初始化, 同时, 定义一个迷宫地图, 对该地图进行初始化, 先判断当前…

数据结构练习——双向链表

http://www.cnblogs.com/-Lei/archive/2012/04/10/2440399.html 复习一下数据结构。。。。说不准下个星期就用上了 只不过写的很简单&#xff0c;没有封装 DoubleLinkList.h #ifndef GUARD_DoubleLinkList_h #define GUARD_DoubleLinkList_h#include <stdio.h>struct Li…

CodeChef - DGCD——树链剖分+差分

【题目描述】 Youre given a tree on N vertices. Each vertex has a positive integer written on it, number on the ith vertex being vi. Your program must process two types of queries :1. Find query represented by F u v : Find out gcd of all numbers on the uniq…

UVa272-TeX中的引号

【题目描述】 传送门 【题目分析】 今天开始刷紫书的题目啦 这道题很简单&#xff0c;需要注意的是cgetchar()需要加上括号&#xff0c;因为赋值语句的优先级比判等低 而且书中说好像最好用整型变量&#xff0c;因为EOF的值为-1&#xff0c;在字符变量中没有这个值。&#xf…

C++中友元(友元函数和友元类)的用法和功能

http://blog.csdn.net/adriano119/article/details/5914443/ 采用类的机制后实现了数据的隐藏与封装&#xff0c;类的数据成员一般定义为私有成员&#xff0c;成员函数一般定义为公有的&#xff0c;依此提供类与外界间的通信接口。但是&#xff0c;有时需要定义一些函数&#x…

线程的终止分离

1.线程的终止 注意该函数是针对用户级别的, 其中 retal 必须指向一个全局变量, 或者是一个 malloc 分配的, 因为如果是线程的局部变量, 当该线程退出时, 其他线程不能得到这个变量, 因为线程的局部变量各自私有 2. 现成的取消 其中thread是线程的 tid 3.线程的等待与分离 (1)…

UVa10082

【题目描述】 传送门 【题目分析】 同样是一道模拟&#xff0c;但是如何巧妙快速的解决仍然不简单。通过这道题告诉我们对于复杂确定的对应关系我们要灵活运用常量数组。 同时还需要注意的一个小问题就是字符串数组中的"//"指的是转义后的单斜杠&#xff0c;如果只…

C语言中的深拷贝和浅拷贝

http://www.cnblogs.com/zhanggaofeng/p/5421804.html C语言中的深拷贝和浅拷贝 //C语言中的深拷贝和浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>typedef struct _student{char name[30];char *title;…

死锁的产生和避免

1.死锁产生的四个必要条件 (1)互斥条件&#xff1a;资源是独占的且排他使用&#xff0c;进程互斥使用资源&#xff0c;即任意时刻一个资源只能给一个进程使用&#xff0c;其他进程若申请一个资源&#xff0c;而该资源被另一进程占有时&#xff0c;则申请者等待直到资源被占有者…

UVa401

【题目描述】 传送门 【题目描述】 嘻嘻&#xff0c;自己做直接AC还是比较开心的。当然有一部分原因是之前看书的时候详细看过这个题的代码&#xff0c;但是这已经快一年了&#xff0c;应该说做出这道题凭借的是自己的能力吧。回过身去看了一下书中的代码发现自己写的不算复…

gethostbyname() 函数说明

https://www.cnblogs.com/cxz2009/archive/2010/11/19/1881611.html gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件#include <netdb.h>#include <sys/socket.h>函数原型struct hostent *gethostbyname(const char *name);这个函数的传入值是域…

求解迷宫最短路径

1. 多通路迷宫初始化 先构建一个多通路迷宫,并且对其初始化 void MazeInitShortPath(Maze* maze) {if(maze NULL){return;}int row 0;int col 0;for(; row < MAX_COL; row){for(col 0; col < MAX_COL; col){maze -> map[row][col] Map[row][col];}printf("…

UVa340

【题目描述】 传送门 【题目分析】 题目理解以后十分简单&#xff0c;但是这题面实在让人自闭&#xff0c;这么简单的题目啦啦啦啦说了那么多&#xff0c;实在是看不懂。&#xff08;幸亏我看了书理解了题目的意思&#xff0c;要不然。。&#xff09;还是要锻炼自己的读题能…

C语言:结构体中一级指针和二级指针的创建与释放示例

http://blog.csdn.net/Bixiwen_liu/article/details/53610952 这几天把C语言巩固了一下&#xff0c;作为一门最基本的编程语言&#xff0c;C语言还是相当基础和非常重要的&#xff0c;个人认为C语言还是很有必要学好吃透的。 今天写的话题是结构体结构体中一级指针和二级指针的…

带环迷宫求最短路径

前面介绍了简单的迷宫求解问题, 今天我们就对带环迷宫求出它的最短路径 1.首先来看一个带环迷宫的简单地图 在这张迷宫地图中,我们规定入口点的位置entry的坐标是 (0, 1), 同时, 我们给入口点传一个非法坐标,作为入口点的前一个位置(-1, -1). 接下来的思路就和上一篇的思路是一…

UVa1583

【题目描述】 传送门 【题目分析】 我以为很简单就写了一个暴力没有想到超时了。应该是T是非常大的所以必须得打表&#xff0c;将所有的结果都储存起来然后直接输出。 以后遇到这种可以一下算出所有结果的多组数据最好还是算出所有的结果然后再输出答案。 【AC代码】 #inc…

C 结构体嵌套一级指针 二级指针 动态分配内存

https://blog.csdn.net/xielinhua88/article/details/51364623 点击打开链接 #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <string.h> #include <stdio.h> //结构体嵌套一级指针 二级指针 动态分配内存 typedef struct _Teacher { int ag…

线程的同步与互斥

1. 互斥量 在Linux 下线程使用的都是局部变量, 而我们知道, 每一个线程都独立拥有自己的一个栈, 而这些局部便令就在栈中,而线程的创建就是为了实现通信, 此时线程之间无法共享这些变量     为了使得线程之间能够共享数据, 一次我们可以创建一个全局变量, 此时线程都在进程…

二级指针与指针数组的关系

http://blog.csdn.net/shuaishuai80/article/details/6129742 #include <stdio.h> void test(char *argv[]); int main(void) { char *argv[3]{{"abcdefg"},{"1234567"},{"q1w2e3r"}}; test(argv); /*调用指针数组…

UVa1584

【题目描述】 传送门 【题目分析】 也是一道简单的模拟题&#xff0c;1A嘿嘿嘿。 再看书发现和书上的做法差不多。 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstd…