1156C - Match Points - CodeForces Solution


binary search greedy sortings ternary search two pointers *2000

Please click on ads to support us..

Python Code:

def solve_one_hypothesis(n, z, x, k):
    for i in range(k):
                if x[n - k + i] - x[i] < z:
            return False
    return True


def my_fn(n, z, x):

    total = 0
    visited = [False for _ in range(len(x))]

    x = sorted(x)

    left = 0
    right = n // 2

    while (right - left) > 5:
        m1 = left + int((right - left) // 2)
                if solve_one_hypothesis(n, z, x, m1):
            left = m1
        else:
            right = m1

            for k in list(range(left, right + 1))[::-1]:
        if solve_one_hypothesis(n, z, x, k):
            return k


s1 = input()
s2 = input()

n, z = [int(v) for v in s1.split(' ')]
x = [int(v) for v in s2.split(' ')]

total = my_fn(n, z, x)
print(total)

C++ Code:

#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long ll;

int main()
{
    FIO
    int n,z,c=0;
    cin>>n>>z;
    int a[n];
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n);
    ll mid = n/2;
    ll x=mid;
    for(int i=0;i<mid&&x<n;)
    {
        if(abs(a[i]-a[x])>=z)
        {
            c++;
            x++;
            i++;
        }
        else x++;
    }
    cout<<c;

    return 0;
}


Comments

Submit
0 Comments
More Questions

740A - Alyona and copybooks
1491A - K-th Largest Value
922B - Magic Forest
922D - Robot Vacuum Cleaner
408B - Garland
1391A - Suborrays
1700C - Helping the Nature
982A - Row
877A - Alex and broken contest
919D - Substring
362B - Petya and Staircases
1535C - Unstable String
1738F - Connectivity Addicts
1342B - Binary Period
1551C - Interesting Story
794B - Cutting Carrot
534B - Covered Path
1278C - Berry Jam
1203A - Circle of Students
1740B - Jumbo Extra Cheese 2
1740A - Factorise N+M
49B - Sum
23A - You're Given a String
1105C - Ayoub and Lost Array
1624E - Masha-forgetful
998B - Cutting
250A - Paper Work
1740C - Bricks and Bags
1130A - Be Positive
465A - inc ARG