1148B - Born This Way - CodeForces Solution


binary search brute force two pointers *1600

Please click on ads to support us..

Python Code:

n,m,ta,tb,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for i in range(n):
    a[i]+=ta
i = 0
MaxTime = -1
j = 0
while i<n and j<m:
    if b[j]>=a[i]:
        if k>0:
            k-=1
            i+=1
        else:
            MaxTime = b[j]+tb
            break
    j+=1
print(MaxTime)

C++ Code:

#include <bits/stdc++.h>

#define _PRINT_ 1
#define _SOLVE_ 1

#if defined(DEBUG) && _PRINT_ == 1
#include "debug.h"
#else
#define print(x, ...) 0
#define debug(x, ...) 0
#define verify(x, ...) 0
#endif

using namespace std;

using ll = long long;
constexpr int inf = 1e9 + 1;
constexpr ll llinf = 1e18 + 1;

/**
 *
 */
void solve() {
    int n, m, ta, tb, k;
    cin >> n >> m >> ta >> tb >> k;
    vector<int> a(n), b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    vector<int> c(m);
    int i = 0, j = 0;
    while (j < m) {
        if (i == n) {
            c[j++] = n;
            continue;
        }
        if (a[i] + ta <= b[j]) {
            i++;
        } else {
            c[j++] = i;
        }
    }

    auto f = [&](int x)->bool {
        if (x <= k) {
            return true;
        }
        for (int i = 0; i < x; i++) {
            if (c[i] + x - i - 1 <= k) {
                return true;
            }
        }
        return false;
    };

    int l = 0, h = m;
    while (l <= h) {
        int x = (l + h) / 2;
        if (f(x)) {
            l = x + 1;
        } else {
            h = x - 1;
        }
    }
    if (h == m) {
        cout << -1;
    } else {
        cout << b[h] + tb;
    }

}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts