363D - Renting Bikes - CodeForces Solution


binary search greedy *1800

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os


def valid(i, j, n, m):
    if i < n and i >= 0 and j >= 0 and j < m:
        return True      return False


def sumn(i, n):
    return (n-i)*(i+n)/2


def sqfun(a, b, c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a


def getprime(num):
    if all(num % i != 0 for i in range(2, int(math.sqrt(num))+1)):
        return True



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]


def get(mid):
    ind=0
    need=0
    for i in range(n-mid,n):
       
        if boymoney[i]<bikeprice[ind]:
            need+=bikeprice[ind]-boymoney[i]
        ind+=1
    return need<=money


def solve():
    global n,m,boymoney,bikeprice,money
    n, m, money = values()
    boymoney = sorted(values())
    bikeprice = sorted(values())
    l=0;r=min(n, m)
    while l<=r:
        mid=l +(r-l)//2
        if get(mid):l=mid+1
        else:r=mid-1
    print(r,max(0,sum(bikeprice[:r])-money))
   
if __name__ == "__main__":
        solve()

C++ Code:

#include<bits/stdc++.h>

#define int int64_t

using namespace std;

int mn_a = 1e9;
bool f(vector<int> &b, vector<int>&p, int a, int x) {

    if ( (x > b.size()) || (x > p.size()))
        return false;

    int btb = x-1;

    for (int i = 0; i < x; i++) {

        if (b[i] >= p[btb]) {
            btb--;
            continue;
        }

        if (b[i] < p[btb]) {
            if (a - (p[btb]-b[i]) >= 0) {
                a -= (p[btb] - b[i]);
                btb--;
                continue;
            } else return false;
            // falla la plata compartida
        }
        
    }

    return true;
}

signed main() {

    int n, m, a;
    cin >> n >> m >> a;

    vector<int> b(n), p(m);
    for (int i = 0; i < n; i++)
        cin >> b[i];

    for (int i = 0; i < m; i++)
        cin >> p[i];


    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    sort(p.begin(), p.end());

    int l = 0, r = n;
    while (l != r) {
        int mid = (l+r+1)/2;
        if (!f(b, p, a, mid))
            r = mid - 1;
        else l = mid;
    }

    int sum = 0;
    for (int i = 0; i < l; i++)
        sum += p[i];

    cout << l << " " << max((sum - a), (int)0) << endl;


    return 0;
}


Comments

Submit
0 Comments
More Questions

1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team