1041D - Glider - CodeForces Solution


binary search data structures two pointers *1700

Please click on ads to support us..

Python Code:

import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, h = map(int, input().split())
u, v = [], []
for _ in range(n):
    x1, x2 = map(int, input().split())
    u.append(x1)
    v.append(x2)
s = [0]
for i in range(n - 1):
    s.append(s[-1] + u[i + 1] - v[i])
la = v[-1]
ls = s[-1]
ans = 0
for i in range(n):
    x1, x2, si = u[i], v[i], s[i]
    if ls - si < h:
        ans = max(ans, la + h + si - ls - x1)
        break
    j = bisect.bisect_left(s, si + h) - 1
    ans = max(ans, v[j] + h + si - s[j] - x1)
print(ans)

C++ Code:

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

#define ll long long
#define tc ll tc;cin>>tc ;while(tc--)
#define li cout<<'\n';
#define simba iostream::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in(arr, n) for(int i = 0 ; i<(n)  ; i++) {cin>>(arr)[i];}
#define out(arr, n) for(int i = 0 ; i<(n)  ; i++) {cout<<(arr)[i]<<" ";} cout<<"\n";
#define sz(x) ((int)(x.size()))
#define rall(v) rbegin(v), rend(v)
#define all(x) x.begin(),x.end()
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;

const int N = 1e5 + 5, INF = INT_MAX, MOD = 1e9 + 7,LOG = 20;
int Xaxis[8]={0,0,-1,1,-1,-1,1,1}; //right,left,up,down
int Yaxis[8]={1,-1,0,0,-1,1,1,-1}; //right,left,up,down
///vector input
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &i: v)
        is >> i;
    return is;
}

//// بسم الله الرحمن الرحيم

void solve() {
    int n, h;
    cin >> n >> h;
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].first >> v[i].second;
    }
    v.push_back({2e9, 2e9}), n++;
    ll l = 0, r = 0, ans = 0, sum = 0;
    while (r < n) {
        while (v[r].first - v[l].first - sum >= h) {
            sum -= (v[l].second - v[l].first);
            l++;
        }
        sum += v[r].second - v[r].first;
        ans = max(ans, sum), r++;
    }
    cout << ans + h;
}
int main() {
    simba
//    tc{
        solve();
//    }
    return 0;
}



	  	    		   	 		 	   	 	 			 	


Comments

Submit
0 Comments
More Questions

53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum