1650E - Rescheduling the Exam - CodeForces Solution


binary search data structures greedy implementation math sortings *1900

Please click on ads to support us..

Python Code:

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

t = int(input())
for tidx in range(t):
    input()
    n, d = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    b = [a[i+1]-a[i]-1 for i in range(n-1)]
    b.append(a[0]-1)
    mu = min(b)
    maxb = max((max(b)-1)//2, d-max(a)-1)
    idx = b.index(mu)     if idx == n-1:
                b[n-1] = d
        b[0] = a[1]-1
        maxb = max(maxb, (a[1]-2)//2)
        print(max(min(min(b), maxb), mu))
    else:
                b2 = b[:]
        if idx>0:
            c = a[idx+1]-a[idx-1]-1
            b[idx-1] = c
            b[idx] = d
        else: 
            c = a[1]-1
            b[n-1] = c
            b[idx] = d
        m = min(min(b), max(maxb, (c-1)//2))
        b = b2
                if idx<n-2:
            c = a[idx+2]-a[idx]-1
            b[idx] = c
            b[idx+1] = d
            m = max(m, min(min(b), max(maxb, (c-1)//2)))
        else: 
                        b[n-2] = d-a[n-2]-1
            m = max(m, min(b))
        print(max(m, mu))
        
        
    
    
    

C++ Code:

#include <bits/stdc++.h>

#define int long long
#define mp make_pair
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

typedef long double ld;
typedef long long ll;

using namespace std;

mt19937 rnd(143);

const ll inf = 1e9;
const ll M = 998'244'353;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;

int n, d;

int cnt(vector<int> &schedule){
    int mx = 0, mn = inf;
    for(int i = 1; i < n; ++i){
        mx = max(mx, schedule[i] - schedule[i - 1] - 1);
        mn = min(mn, schedule[i] - schedule[i - 1] - 1);
    }
    return min(mn, max(d - schedule.back() - 1, (mx - 1) / 2));
}

void solve(int test_case) {
    cin >> n >> d;
    vector<int> a(n + 1);
    int mn = d, min_pos = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(a[i] - a[i - 1] - 1 < mn){
            mn = a[i] - a[i - 1] - 1;
            min_pos = i;
        }
    }
    vector<int> schedule;
    for(int i = 0; i <= n; ++i){
        if(i != min_pos){
            schedule.push_back(a[i]);
        }
    }
    int ans = cnt(schedule);
    if(min_pos > 1){
        schedule[min_pos - 1] = a[min_pos];
    }
    ans = max(ans, cnt(schedule));
    cout << ans;
}

bool multi = true;

signed main() {
    //freopen("in.txt", "r", stdin);
    //freopen("in.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    if (multi) {
        cin >> t;
    }
    for (int i = 1; i <= t; ++i) {
        solve(i);
        cout << "\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST