1901A - Line Trip - CodeForces Solution


math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define pb push_back
#define int ll
#define sortv(arr) sort(arr.begin(), arr.end())
#define sorta(arr, n) sort(arr, arr + n)
#define get_inout_fast            \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define print(x) cout << x << endl;
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vvi vector<vector<int>>
#define vvs vector<vector<string>>
#define vvc vector<vector<char>>
#define loop(n) for (int i = 0; i < n; i++)
#define loop1(n) for (int j = 0; j < n; j++)
#define all(v) v.begin(), v.end()

int n;
bool poss(vector<int> &nums, int x, int dt)
{
    int curr = 0;
    for (int i = 0; i < n; i++)
    {
        int dis = nums[i] - curr;
        if (dis > x)
            return false;
        curr = nums[i];
    }
    int dis = 2*(dt - nums[n - 1]);
    if (dis > x)
        return false;
    return true;
}

void accio()
{
   
    int x;
    cin >> n >> x;
    vi a(n);
    loop(n) cin >> a[i];
    int low = 0, high = 1e9, ans = INT_MAX;
    while (low <= high)
    {
        int md = (low + high)/2;
        if (poss(a, md, x))
        {
            ans = md;
            high = md - 1;
        }
        else
        {
            low = md + 1;
        }
    }
    cout << ans << endl;
}
signed main()
{
    get_inout_fast
#ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t = 1;
    cin >> t;
    while (t--)
        accio();
    return 0;
}


Comments

Submit
0 Comments
More Questions

677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys