D. Cutting Out 二分

题意是给你n个数字的序列,让你从中找含k个数字的序列,要求这k个数字要尽可能多次的从n个数字的序列中减去。

解法就是从1到n,二分查找可以删除的最大次数。

http://codeforces.com/contest/1077/problem/D

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int book[maxn];
int box[maxn];
int n,k;
int fun(int cnt)
{
    int top=0;
    for(int i=1; i<maxn; i++)
    {
        for(int j=1; j<=book[i]/cnt; j++)
        {
            box[++top]=i;
        }
    }
    return top;
}
int main()
{
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++)
    {
        int t;
        scanf("%d", &t);
        book[t]++;
    }
    int l,r;
    l=1,r=n;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(fun(mid)>=k)
            l=mid+1;
        else
            r=mid-1;
    }
    fun(l-1);
    printf("%d", box[1]);
    for(int i=2; i<=k; i++)
        printf(" %d", box[i]);
}

 

转载于:https://www.cnblogs.com/dongdong25800/p/9978457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值