Till I Collapse (Codeforces 786C)
Tags: Others, divide and conquer

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.

Note: For the first sample testcase, some optimal ways of dividing army into squads for each k are:  [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] For the second testcase, some optimal ways of dividing army into squads for each k are:  [1], [5], [7], [8], [1], [7], [6], [1]  [1, 5], [7, 8], [1, 7], [6, 1]  [1, 5, 7], [8], [1, 7, 6, 1]  [1, 5, 7, 8], [1, 7, 6, 1]  [1, 5, 7, 8, 1, 7, 6, 1]  [1, 5, 7, 8, 1, 7, 6, 1]  [1, 5, 7, 8, 1, 7, 6, 1]  [1, 5, 7, 8, 1, 7, 6, 1] 

Sample Inputs:
5
1 3 4 3 3

8
1 5 7 8 1 7 6 1

Sample Outputs:
4 2 1 1 1

8 4 3 2 1 1 1 1

Solution (Python):
import os, sys, collections, heapq, itertools
if os.path.exists('in.txt'):
    file = open('in.txt')
    input = file.readline
else:
    input = sys.stdin.readline
from functools import reduce
from itertools import accumulate, chain, combinations, count
from itertools import groupby, permutations, product, zip_longest
from heapq import heapify, heappush, heappop, heapreplace, merge, nlargest, nsmallest
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect_right
from math import comb, floor, ceil, inf, gcd, sqrt
import functools
readi = lambda : int(input())
readis = lambda : list(map(int, input().split()))
reads = lambda : input().rstrip()
readss = lambda : input().split()

def solution():
    n = readi()
    a = readis()
    res = [0] * (n + 1)
    r = len(set(a))
    res[r] = 1
    res[1] = len(list(groupby(a)))
    q = [(1, r)]
    c = [0] * (n + 1)

    def f(k):
        m = 0
        i = 0
        while i < n:
            m += 1
            cnt = 0
            while i < n and cnt <= k:
                if c[a[i]] != k << 20 | m:
                    c[a[i]] = k << 20 | m
                    cnt += 1
                i += 1
            if cnt > k:
                i -= 1
        return m
    while q:
        (l, r) = q.pop()
        mid = (l + r) // 2
        res[mid] = f(mid)
        if mid - l > 1 and res[l] != res[mid]:
            q.append([l, mid])
        if r - mid > 1 and res[mid] != res[r]:
            q.append([mid, r])
    for i in range(2, n + 1):
        if not res[i]:
            res[i] = res[i - 1]
    print(*res[1:])
for i in range(1):
    solution()

Solution (Python):
import os, sys, collections, heapq, itertools
if os.path.exists('in.txt'):
    file = open('in.txt')
    input = file.readline
else:
    input = sys.stdin.readline
from functools import reduce
from itertools import accumulate, chain, combinations, count
from itertools import groupby, permutations, product, zip_longest
from heapq import heapify, heappush, heappop, heapreplace, merge, nlargest, nsmallest
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect_right
from math import comb, floor, ceil, inf, gcd, sqrt
import functools
readi = lambda : int(input())
readis = lambda : list(map(int, input().split()))
reads = lambda : input().rstrip()
readss = lambda : input().split()

def solution():
    n = readi()
    a = readis()
    res = [0] * (n + 1)
    r = len(set(a))
    res[r] = 1
    res[1] = len(list(groupby(a)))
    q = [(1, r)]
    c = [0] * (n + 1)

    def f(k):
        m = 0
        i = 0
        while i < n:
            m += 1
            cnt = 0
            while i < n and cnt <= k:
                if c[a[i]] != k << 20 | m:
                    c[a[i]] = k << 20 | m
                    cnt += 1
                i += 1
            if cnt > k:
                i -= 1
        return m
    while q:
        (l, r) = q.pop()
        mid = (l + r) // 2
        res[mid] = f(mid)
        if mid - l > 1 and res[l] != res[mid]:
            q.append([l, mid])
        if r - mid > 1 and res[mid] != res[r]:
            q.append([mid, r])
    for i in range(2, n + 1):
        if not res[i]:
            res[i] = res[i - 1]
    print(*res[1:])
for i in range(1):
    solution()

Solution (Python):
import sys
from collections import deque
from math import log2
from types import GeneratorType
RI = lambda : map(int, sys.stdin.buffer.readline().split())
RILST = lambda : list(RI())
print = lambda d: sys.stdout.write(str(d) + '\n')
MOD = 10 ** 9 + 7
PROBLEM = 'https://codeforces.com/contest/786/problem/C\n\n输入 n(1≤n≤1e5) 和长为 n 的数组 a(1≤a[i]≤n)。\n\n定义 f(k) 为最小的 m，使得存在一种将 a 划分成 m 段的方式，其中每段的不同数字个数都不超过 k。\n输出 f(1), f(2), ... ,f(n)。\n输入\n5\n1 3 4 3 3\n输出\n4 2 1 1 1\n\n输入\n8\n1 5 7 8 1 7 6 1\n输出\n8 4 3 2 1 1 1 1\n'
''
''

class FastIO:

    def __init__(self):
        return

    @staticmethod
    def _read():
        return sys.stdin.readline().strip()

    def read_int(self):
        return int(self._read())

    def read_float(self):
        return float(self._read())

    def read_ints(self):
        return map(int, self._read().split())

    def read_floats(self):
        return map(float, self._read().split())

    def read_ints_minus_one(self):
        return map(lambda x: int(x) - 1, self._read().split())

    def read_list_ints(self):
        return list(map(int, self._read().split()))

    def read_list_floats(self):
        return list(map(float, self._read().split()))

    def read_list_ints_minus_one(self):
        return list(map(lambda x: int(x) - 1, self._read().split()))

    def read_str(self):
        return self._read()

    def read_list_strs(self):
        return self._read().split()

    def read_list_str(self):
        return list(self._read())

    @staticmethod
    def st(x):
        return sys.stdout.write(str(x) + '\n')

    @staticmethod
    def lst(x):
        return print(*x)

    @staticmethod
    def round_5(f):
        res = int(f)
        if f - res >= 0.5:
            res += 1
        return res

    @staticmethod
    def max(a, b):
        return a if a > b else b

    @staticmethod
    def min(a, b):
        return a if a < b else b

    @staticmethod
    def bootstrap(f, queue=[]):

        def wrappedfunc(*args, **kwargs):
            if queue:
                return f(*args, **kwargs)
            else:
                to = f(*args, **kwargs)
                while True:
                    if isinstance(to, GeneratorType):
                        queue.append(to)
                        to = next(to)
                    else:
                        queue.pop()
                        if not queue:
                            break
                        to = queue[-1].send(to)
                return to
        return wrappedfunc

    def ask(self, lst):
        self.lst(lst)
        sys.stdout.flush()
        res = self.read_int()
        return res

    @staticmethod
    def accumulate(nums):
        n = len(nums)
        pre = [0] * (n + 1)
        for i in range(n):
            pre[i + 1] = pre[i] + nums[i]
        return pre

def solve1():
    (n,) = RI()
    a = RILST()
    ans = [0] * (n + 1)

    def f(k):
        m = 1
        s = set()
        for v in a:
            s.add(v)
            if len(s) > k:
                s = {v}
                m += 1
        return m

    def dfs(l, r):
        if not ans[l]:
            ans[l] = f(l)
        if not ans[r]:
            ans[r] = f(r)
        if l + 1 >= r:
            return
        if ans[l] == ans[r]:
            for i in range(l + 1, r):
                ans[i] = ans[l]
            return
        mid = (l + r) // 2
        dfs(l, mid)
        dfs(mid, r)
    dfs(1, n)
    print(*ans[1:])

def solve3():
    (n,) = RI()
    a = RILST()
    ans = [0] * (n + 1)
    time = [0] * (n + 1)
    clock = 1

    def f(k):
        nonlocal clock
        clock += 1
        m = 1
        cnt = 0
        for v in a:
            if time[v] == clock:
                continue
            cnt += 1
            if cnt > k:
                m += 1
                clock += 1
                cnt = 1
            time[v] = clock
        return m

    def dfs(l, r):
        if not ans[l]:
            ans[l] = f(l)
        if not ans[r]:
            ans[r] = f(r)
        if l + 1 >= r:
            return
        if ans[l] == ans[r]:
            for i in range(l + 1, r):
                ans[i] = ans[l]
            return
        mid = (l + r) // 2
        dfs(l + 1, mid)
        dfs(mid + 1, r - 1)
    dfs(1, n)
    print(*ans[1:])

def solve2():
    (n,) = RI()
    a = RILST()
    ans = [0] * (n + 1)
    time = [0] * (n + 1)
    clock = 1

    def f(k):
        nonlocal clock
        clock += 1
        m = 1
        cnt = 0
        for v in a:
            if time[v] == clock:
                continue
            cnt += 1
            if cnt > k:
                m += 1
                clock += 1
                cnt = 1
            time[v] = clock
        return m

    def dfs(l, r):
        if not ans[l]:
            ans[l] = f(l)
        if not ans[r]:
            ans[r] = f(r)
        if l + 1 >= r:
            return
        if ans[l] == ans[r]:
            for i in range(l + 1, r):
                ans[i] = ans[l]
            return
        mid = (l + r) // 2
        dfs(l, mid)
        dfs(mid, r)
    dfs(1, n)
    print(' '.join(map(str, ans[1:])))
clock = 1

def solve():
    (n,) = RI()
    a = RILST()
    ans = [0] * (n + 1)
    time = [0] * (n + 1)

    def f(k):
        global clock
        clock += 1
        m = 1
        cnt = 0
        for v in a:
            if time[v] == clock:
                continue
            cnt += 1
            if cnt > k:
                m += 1
                clock += 1
                cnt = 1
            time[v] = clock
        return m
    L = min(2 * int(n ** 0.5), n - 1)
    for i in range(1, L + 1):
        ans[i] = f(i)
    q = [(L, n)]
    while q:
        nq = []
        for (l, r) in q:
            if not ans[l]:
                ans[l] = f(l)
            if not ans[r]:
                ans[r] = f(r)
            if l + 1 >= r:
                continue
            if ans[l] == ans[r]:
                for i in range(l + 1, r):
                    ans[i] = ans[l]
                continue
            mid = l + r >> 1
            nq.append((l, mid))
            nq.append((mid, r))
        q = nq
    print(' '.join(map(str, ans[1:])))
if __name__ == '__main__':
    (n,) = RI()
    a = RILST()
    ans = [0] * (n + 1)
    time = [0] * (n + 1)
    clock = 1
    L = min(int(log2(n)), n - 1)
    for i in range(1, L + 1):
        clock += 1
        m = 1
        cnt = 0
        for v in a:
            if time[v] == clock:
                continue
            cnt += 1
            if cnt > i:
                m += 1
                clock += 1
                cnt = 1
            time[v] = clock
        ans[i] = m
    q = [(L, n)]
    while q:
        nq = []
        for (l, r) in q:
            if not ans[l]:
                clock += 1
                m = 1
                cnt = 0
                for v in a:
                    if time[v] == clock:
                        continue
                    cnt += 1
                    if cnt > l:
                        m += 1
                        clock += 1
                        cnt = 1
                    time[v] = clock
                ans[l] = m
            if not ans[r]:
                clock += 1
                m = 1
                cnt = 0
                for v in a:
                    if time[v] == clock:
                        continue
                    cnt += 1
                    if cnt > r:
                        m += 1
                        clock += 1
                        cnt = 1
                    time[v] = clock
                ans[r] = m
            if l + 1 >= r:
                continue
            if ans[l] == ans[r]:
                for i in range(l + 1, r):
                    ans[i] = ans[l]
                continue
            mid = l + r >> 1
            nq.append((l, mid))
            nq.append((mid, r))
        q = nq
    print(' '.join(map(str, ans[1:])))