1613C - Poisoned Dagger - CodeForces Solution


binary search *1200

Please click on ads to support us..

Python Code:

def dealt(k, b):
    lost = 0
    for x in b:
        if k <= x:
            continue
        else:
            lost += k - x
    if k * n - lost >= h:
        return True
    else:
        return False

T = int(input())
for _ in range(T):
    n, h = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    for i in range(n-1):
        b.append(a[i+1] - a[i])
    low = 0
    if h % n == 0:
        low = h // n
    else:
        low = h // n + 1
    high = h
    while low <= high:
        mid = (low + high) // 2
        ans = dealt(mid, b)
        ans_1 = dealt(mid-1, b)
        if ans and not ans_1:
            print(mid)
            break
        elif ans:
            high = mid - 1
        else:
            low = mid + 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
bool is_possible(vector<ll>& v,ll n,ll h){
   ll ct = 0;
   for(int i=1;i<v.size();i++){
      ct += min(n,(v[i]-v[i-1]));
   }
   ct += n;
   if(ct>=h) return true;
   else return false;
}
void solve(){
   ll n,h;
   cin>>n>>h;
   vector<ll> v(n);
   for(ll i=0;i<n;i++) cin>>v[i];
   ll lo = 1,hi = h,ans=h;
   while(lo<=hi){
      ll mid = (hi+lo)/2;
      if(is_possible(v,mid,h)){
         ans = min(mid,ans);
         hi = mid-1;
      }
      else lo = mid+1;
   }
   cout<<ans<<endl;
}
int main()
{
   IOS
   //precomp();
   ll t;
   cin>>t;
   while(t--) solve();
   return 0;
}


Comments

Submit
0 Comments
More Questions

964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch