1907D - Jumping Through Segments - CodeForces Solution


binary search constructive algorithms *1400

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
//#include<algorithm>
//#include<queue>
#define ll long long
#define QMax ((1<<22)-1)
#define rep(a, b) for (register int a = 0; a < b; a++)
#define rep2(a,c,b) for(ll a=c;a<b;a++)
#define myout(a) rep(i,a.size()) cout<<a[i]<<" ";cout<<"\n";
#define myout2(a) rep(i,a.size()){ rep(j,a[i].size())cout<<a[i][j]<<" ";cout<<"\n";}
#define mmin(a,b) (a>b? b:a)
#define mmax(a,b) (a<b? b:a)
//#define INF 1000000000

#include<algorithm>


using namespace std;

#define INF 1<<30

struct P2 {
    
    ll l,r;
};
vector<P2> A;
ll n;

bool chk(ll k) {

    P2 x = { 0,0 };

    rep(i, n) {
        ll s = x.l - k, e = x.r + k;
        x.l = mmax(s, A[i].l);
        x.r= mmin(e, A[i].r);
        if (x.l > x.r) return false;
    }
    return true;

}
int main()
{
  // freopen("a.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll TT; cin >> TT;

    while (TT--) {
        cin >> n;
        A.resize(n);
        rep(i, n) cin >> A[i].l >> A[i].r;



        ll ub = INF, lb = -1;
        ll mid;

        while (ub > lb + 1) {
            mid = (ub + lb )/ 2;
            if (chk(mid))
                ub = mid;
            else
                lb = mid;
        }
        ll ans = ub;

        cout << ans << "\n";
    }

    

}


Comments

Submit
0 Comments
More Questions

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut