1. 程式人生 > >Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

任意門:http://codeforces.com/contest/1077/problem/D

D. Cutting Out time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output

You are given an array ss consisting of 

">nn integers.

You have to find any array tt of length kk such that you can cut out maximum number of copies of array tt from array ss.

Cutting out the copy of tt means that for each element titi of array tt you have to find 

ti">titi in ss and remove it from ss. If for some titi you cannot find such element in ss, then you cannot cut out one more copy of tt. The both arrays can contain duplicate elements.

For example, if s=[1,2,3,2,4,3,1]s=[1,2,3,2,4,3,1] and 

k=3">k=3k=3 then one of the possible answers is t=[1,2,3]t=[1,2,3]. This array tt can be cut out 22 times.

Your task is to find such array tt that you can cut out the copy of tt from ss maximum number of times. If there are multiple answers, you may choose any of them.

Input

The first line of the input contains two integers nn and kk (1kn21051≤k≤n≤2⋅105) — the number of elements in ss and the desired number of elements in tt, respectively.

The second line of the input contains exactly nn integers s1,s2,,sns1,s2,…,sn (1si21051≤si≤2⋅105).

Output

Print kk integers — the elements of array tt such that you can cut out maximum possible number of copies of this array from ss. If there are multiple answers, print any of them. The required array tt can contain duplicate elements. All the elements of tt (t1,t2,,tkt1,t2,…,tk) should satisfy the following condition: 1ti21051≤ti≤2⋅105.

Examples input Copy
7 3
1 2 3 2 4 3 1
output Copy
1 2 3 
input Copy
10 4
1 3 1 3 10 3 7 7 12 3
output Copy
7 3 1 3
input Copy
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
output Copy
1 1 
Note

The first example is described in the problem statement.

In the second example the only answer is [7,3,1,3][7,3,1,3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 22.

In the third example the array tt can be cut out 55 times.

 

題意概括:

在長度為N的序列中找出K個元素的子序列,滿足這個子序列在序列中最多(子序列 不要求有序,連續);

解題思路:

一開始的思想:記錄每種數出現的次數,按照降序排序,高出現次數的補低出現次數的,貪心最大化最小值。(wa,程式碼實現不好)

二分+排序(其實看到要最大化最小值就應該想到了的。。。)

排序還是記錄原N序列的每種數的出現次數,按照降序排序;

二分最低出現的次數,判斷條件是以這個最低標準是否能填充滿 子序列 K。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define LL long long
 6 using namespace std;
 7 const int MAXN = 1e6+10;
 8 LL sum, mx, x;
 9 int K, N, ct, cnt;
10 int ans[MAXN], ans0, an[MAXN];
11 int l = 1, r;
12 int p[MAXN];
13 
14 struct date
15 {
16     int num, t;
17 }a[MAXN];
18 
19 bool cmp(date a, date b)
20 {
21     return a.t > b.t;
22 }
23 
24 bool judge(int tms)
25 {
26     cnt = 0;
27     for(int i = 1; i <= ct; i++){
28         int tmp = a[i].t;
29         if(tmp < tms) break;        //因為已經排序過了
30         while(tmp >= tms){
31             tmp-=tms;
32             ans[++cnt] = a[i].num;
33         }
34     }
35     return cnt>=K;
36 }
37 
38 int main()
39 {
40     scanf("%d%d", &N, &K);
41     r = N;
42     for(int i = 1; i <= N; i++){
43         scanf("%d", &x);
44         if(mx < x) mx = x;
45         p[x]++;
46     }
47     for(int i = 1; i <= mx; i++){
48         if(p[i]){
49             a[++ct].num = i;
50             a[ct].t = p[i];
51         }
52     }
53     sort(a+1, a+1+ct, cmp);
54     int mid;
55     while(l<=r){
56         mid = (l+r)>>1;
57         if(judge(mid)){
58             an[0] = 0;
59             for(int i = 1; i <= cnt; i++){
60                 an[++an[0]] = ans[i];
61                 l = mid+1;
62             }
63         }
64         else r = mid-1;
65     }
66     for(int i = 1; i <= K; i++){
67         printf("%d ", an[i]);
68     }
69     puts("");
70     return 0;
71 }
View Code

 

相關推薦

Codeforces Round #521 (Div. 3) D. Cutting Out 二分+排序

任意門:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory limit per test 25

Codeforces Round #521 (Div. 3) D - Cutting Out(二分答案)

D. Cutting Out time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You a

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

題解 題目大意 給你一個序列 讓你找到一個長度為k的序列 在原序列當中出現的次數最多 滿足單調性 二分出現次數 然後根據出現次數隨意輸出一個序列 AC程式碼 #include <stdio.h> #include <bits/stdc++.h> usi

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

題意:傳送門 題意: 給出一個長度為n的字串,要求每次刪除k個字串,使剩下的字元最少的長度為k的字串。 思路: 求出最小的刪除次數和最大的刪除字數然後進行二分,然後求出最大的刪除次數。 為了操作方便,這裡進行了離散化。 程式碼如下: #include <cstdio>

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

http://codeforces.com/contest/1077/problem/D   You are given an array ss consisting of nn integers. You have to find a

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

題意:傳送門 題意: 給出一個長度為n的字串,要求每次刪除k個字串,使剩下的字元最少的長度為k的字串。 思路: 求出最小的刪除次數和最大的刪除字數然後進行二分,然後求出最大的刪除次數。 為了操作方便,這裡進行了離散化。 程式碼如下: #include <cst

CF #521 div.3 D Cutting Out

Cutting Out   題目大意: 選出 k 個數,要求這 k 個數組成的組合在原序列中出現次數最多,輸出方案。   題解: div.3 都不會做了,要加強了啊!!! 二分出現的次數 mid。然後用每個數出現的次數 整除 mid = x, x 就是對序列個數的貢獻

Codeforces Round #521 (Div. 3) D

自己手撕WA 19 #include<iostream> #include<stdio.h> #include<queue> #include<algorithm> using namespace std; int a[200010]; int a

Codeforces Round #521 (Div. 3) A 模擬 B 貪心 C模擬 D 二分 E 二分+排序

A Code: #include <bits/stdc++.h> #define LL long long using namespace std; int main(){ int T ; cin >> T ; int a , b , k ;

Codeforces Round #521 (Div. 3) A B C D E

A. Frog Jumping 題目連結:https://codeforces.com/contest/1077/problem/A 題目大意:t組測試資料,從0開始,向前跳a,向後跳b,跳k次,輸出最後所在的位置。 題解:大水題,直接輸出就行了 int main() { std:

Codeforces Round #521 (Div. 3) A 模擬 B 貪心 C模擬 D 二分 E 二分+排序

A Code: #include <bits/stdc++.h> #define LL long long using namespace std; int main(){ int T

Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression

tmp res spa ssi com png true inf false D. Almost Arithmetic Progression Example 1 input 4 24 21 14 10 output 3 Example 2 input 2 500 5

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

equals tegra names size AR tin ++ include ESS Codeforces Round #486 (Div. 3) D. Points and Powers of Two 題目連接: http://codeforces.com/gro

Codeforces Round #501 (Div. 3) D Walking Between Houses

端點 你是 pre n) main 選區 賽後 str 但是 翻譯 給你一條數軸(出題人很喜歡數軸啊),上面有排列著\(n\)個點,編號為\(1\)到\(n\)(你開始在\(1\))。每次你要從一個點移動到另一個點(可以重復)。移動距離為兩點坐標之差的絕對值,問你是否能否在

Codeforces Round #506 (Div. 3) D-F

with while 長度 難度 滿足 clu || ted return Codeforces Round #506 (Div. 3) (中等難度) 自己的做題速度大概只嘗試了D題,不過TLE D. Concatenated Multiples 題意 數組a[],長度n

Codeforces Round #515 (Div. 3) D. Boxes Packing

   比賽的時候這題的題意一直沒看懂,後面才明白意思就是有n個物品,然後要求最多可以挑選出多少物品,挑選過程是從第一給物品開始往右邊挑選,對於每一個物品,如果目前的盒子剩餘空間是>=該物品的體積的就直接放進盒子,繼續挑選下一個物品,若目前的盒子剩餘空間是<該物

Codeforces Round #515 (Div. 3) D(模擬)

題意:有m個箱子,每個箱子容量為k,問你最多能裝多少個物品,你只能依次捨棄前面的,後面的必須全部裝完。 思路:題目意思是裝過東西的箱子如果你又拿了一個箱子,那麼以前的箱子就裝不了了,即使還能裝。所以倒著遍歷物品,模擬一下就行了。 #include<bits/stdc++.h> u

Codeforces Round #521 (Div. 3) CDE

C 問刪掉在陣列中一個數後,剩下的陣列中,是否存在一個數時其它所有數的和。 遍歷每一個數,假設刪掉它後,剩下的數中取一個最大值,看他的值是不是陣列之和的一半,是的話就成立。 1 #include <bits/stdc++.h> 2 #define ll long long 3

Codeforces Round #521 (Div. 3)

題目連結:http://codeforces.com/contest/1077 A.Frog Jumping 解題思路:作差再判斷最後是否還需再一次向右跳即可。 AC程式碼: 1 #include<bits/stdc++.h> 2 using namespace std;

codeforces1077C Good Array(Codeforces Round #521 (Div. 3))

傳送門:http://codeforces.com/contest/1077/problem/C 吐槽:忘記特判2的情況被hack了,肥宅大哭.jpg 程式碼: #include<cstdio> #include<cmath> #include<algo