1077D - Cutting Out - CodeForces Solution


binary search sortings *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
const double eps = 1e-9;
//#define int long long
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll fastpower(ll x,ll n){ll res=1;while(n){if(n&1)res=res*x;x=x*x;n=n/2;}return res;}
double roundd(double a){
    if(fabs((a * 100.0) - (int)(a * 100) - 0.5) > eps && (int)(a * 1000) % 10 >= 5)
        return ceil(a * 100.0) / 100.0;
    else
        return floor(a * 100.0) / 100.0;;
}
bool isprime(int n)
{

    if(n==2)return true;
    if(n<2||n%2==0)return false;
    for (long long  i = 3; i*i<=n; i+=2) {
        if (n % i == 0)
            return false;
    }
    return true;
}
int qqx[]={0,0,-1,1,1,-1,1,-1};
int qqy[]={1,-1,0,0,1,1,-1,-1};
bool valid(int i,int j,int n,int m)
{
    if(i>=0&&j>=0&&i<n&&j<m)return true;
    return false;
}
ll gcd(ll a,ll b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}
int inf=1e9+7;
#define F first
#define S second
#define int ll
int MOD =1e9+7;
bool compare(const pair<int, int>& a, const pair<int, int>& b) {
    return a.first > b.first;
}
bool check(int pos,int row)
{
    if(pos>=1&&pos<=row)
        return true;
    return false;
}
const int siz = 2e5 + 10;
int n,k;
int f[siz];
bool can(int m,vector<int>&b)
{
    int cnt=k;
    for(int i=0;i<siz; i++)
    {
        int temp = f[i];
        while(temp>=m){
            b.push_back(i);
            temp-=m;
            cnt--;
            if(cnt==0){
                return true;
            }
        }
    }
    return false;
}
void out()
{
    cin>>n>>k;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    for(auto &i:a)f[i]++;
    int l=1,r=1e6;
    int ans = 1;
    vector<int>res;
    vector<int>b;
    while(r>=l)
    {
        b.clear();
        int m=l+(r-l)/2;
        if(can(m,b))
        {
            res = b;
            ans=m;
            l=m+1;
        }
        else{
            r=m-1;
        }
    }
    //cout<<ans;
    for(auto &i : res )cout<<i<<" ";



}
signed main()
{
    //freopen("", "r", stdin);
    cin.tie(0);
    cin.sync_with_stdio(0);
    int testCase=1;
    //cin>>testCase;
    while(testCase--)
    {
        out();
        cout<<"\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs