1. 程式人生 > >Codeforces Round #521 (Div. 3) D. Cutting Out

Codeforces Round #521 (Div. 3) D. Cutting Out

題解

題目大意 給你一個序列 讓你找到一個長度為k的序列 在原序列當中出現的次數最多

滿足單調性 二分出現次數 然後根據出現次數隨意輸出一個序列

AC程式碼

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
int a[MAXN];
int N, K;

bool check(int m)
{
	int cnt = K;
for (int i = 1; i < MAXN; i++) cnt -= a[i] / m; if (cnt <= 0) return true; return false; } int main() { #ifdef LOCAL freopen("C:/input.txt", "r", stdin); #endif int x; cin >> N >> K; for (int i = 1; i <= N; i++) scanf("%d", &x), a[x]++; int l = 1, r = N, ans = 1;
//切次數 while (l <= r) { int m = l + r >> 1; if (check(m)) l = m + 1, ans = m; else r = m - 1; } int fst = 1; for (int i = 1; i < MAXN; i++) { for (int j = 0; j < a[i] / ans; j++) { fst ? fst = 0 : putchar(' '); printf("%d", i); K--; if (K <= 0) cout <<
endl, exit(0); } } return 0; }