zoukankan      html  css  js  c++  java
  • 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 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=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.

    • To cut out the first copy of tt you can use the elements [1,2––,3,2,4,3––,1––][1,2_,3,2,4,3_,1_] (use the highlighted elements). After cutting out the first copy of tt the array ss can look like [1,3,2,4][1,3,2,4].
    • To cut out the second copy of tt you can use the elements [1––,3––,2––,4][1_,3_,2_,4]. After cutting out the second copy of tt the array ss will be [4][4].

    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
  • 相关阅读:
    pdo连接的时候设置字符编码是这样的
    mysql8.0+修改用户密码
    mysql账户添加远程访问
    php中的动态变量的一个应用
    redis scan迭代模糊匹配
    限制用户频繁提交
    js判断checkbox是否选中
    mysql 分组取每个组的前几名的问题
    Yii框架和Vue的完美结合构建前后端分离项目
    JS发送跨域Post请求出现两次请求的解决办法
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9973185.html
Copyright © 2011-2022 走看看