1041C - Coffee Break - CodeForces Solution


binary search data structures greedy two pointers *1600

Please click on ads to support us..

Python Code:

import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binary_search(c1, c2):
    m = (c1 + c2 + 1) // 2
    while abs(c1 - c2) > 1:
        m = (c1 + c2 + 1) // 2
        if ok(m):
            c2 = m
        else:
            c1 = m
    m = max(1, m - 1)
    while not ok(m):
        m += 1
    return m

def ok(m):
    for i in range(n - m):
        if b[i + m] - b[i] <= d:
            return False
    return True

n, m, d = map(int, input().split())
a = list(map(int, input().split()))
b = list(a)
b.sort()
l = binary_search(0, n)
h = []
for i in range(n):
    heapq.heappush(h, (a[i], i))
ans = [0] * n
now = 0
while h:
    _, i = heapq.heappop(h)
    ans[i] = now + 1
    now += 1
    now %= l
print(l)
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,l,x;
    cin>>n>>l>>x;
    int a[n];
    set<int>s;
    vector<pair<int,int>>v;
    for(int i=0;i<n;i++){
        cin>>a[i];
        s.insert(a[i]);
    }
    map<int,int>mp;
    int day=1;
    int filled=0;
    auto it = s.begin();
    while(filled<n){
        int k = *(it);
        mp[k] = day;
        filled++;
        s.erase(k);
        if(filled==n)break;
        it = s.lower_bound(k+x+1);
        //cout<<*(it)<<" ";
        if(it==s.end()){
            it = s.begin();
            day++;
        }
    }
    cout<<day<<endl;
    for(auto i:a){
        cout<<mp[i]<<" ";
    }
    cout<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books