580B - Kefa and Company - CodeForces Solution


binary search sortings two pointers *1500

Please click on ads to support us..

Python Code:

from typing import NamedTuple


class Friend(NamedTuple):
    money: int
    friendship_score: int


def split_in_groups(
    array: "list[Friend]",
    target: int,
):
    i = 0
    freezed_index = 0
    border = len(array) - 1
    max_score = 0
    score = 0

    while i <= border:
        difference = array[i].money - array[freezed_index].money

        while difference >= target:
            score -= array[freezed_index].friendship_score
            freezed_index += 1
            difference = array[i].money - array[freezed_index].money
        
        score += array[i].friendship_score
        
        if score > max_score:
            max_score = score
        i += 1
        

    return max_score


def collect_input():
    num_friends, target = [int(inputted) for inputted in input().split(" ")]
    friend_list = []
    for _ in range(num_friends):
        money, score = [int(inputted) for inputted in input().split(" ")]
        friend = Friend(money, score)
        friend_list.append(friend)
    friend_list.sort(key=lambda friend: friend.money)
    return target, friend_list


if __name__ == "__main__":
    target, friend_list = collect_input()
    max_score = split_in_groups(friend_list, target)
    print(max_score)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int,int>

#define pbb push_back
#define con continue
#define br break
#define len length
#define F first
#define S second
#define mem(dp) memset(dp,-1,sizeof(dp))
#define all(x) x.begin(),x.end()

#define sp system("pause");
#define deb cout << "\n*** HERE ***\n"
#define nl cout << "\n"

const int N = 10e5 + 10;
const int mod = 1e9 + 7;
#define fastIO ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

ll power(ll a, ll b) { a %= mod; ll res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(a % b, b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }


/*       HERE CODE STARTS    */

void solve()
{
    int n, d; cin >> n >> d;
    vector<pi>arr;
    for (int i = 0; i < n; i++)
    {
        int a, b; cin >> a >> b;
        arr.pbb({ a,b });
    }
    sort(all(arr));
    int st = 0;
    ll fs = 0;
    ll res = 0;
    for(int i = 0;i<n;i++)
    {
        fs+=arr[i].S;
        while(st < i && abs(arr[st].F - arr[i].F)>=d)
        {
            fs-=arr[st].S;
            st++;
        }
        res = max(res,fs);
    }



    cout << res << endl;
}



int main()
{
    fastIO;
    int t = 1;
    while (t--) solve();



    return 0;
}


Comments

Submit
0 Comments
More Questions

1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends