991C - Candies - CodeForces Solution


binary search implementation *1500

Please click on ads to support us..

Python Code:

def solveDQ(state, base_cases, get_sub_problems, merge):
  for condition, result in base_cases:
    if condition(state):
      return result(state)
  
  sub_problems = get_sub_problems(state)

  sub_results = [
    solveDQ(sub_problem, base_cases, get_sub_problems, merge) 
    for sub_problem in sub_problems
  ]

  result = merge(sub_results)

  return result

def binary_search_first_high(l, r, condition):
  def get_sub_problems(state):
        l, r = state
    mid = (l + r) // 2
    if condition(mid):
      return [(l, mid)]
    else:
      return [(mid + 1, r)]

  state = (l, r)
  base_cases = [(lambda s: s[0] == s[1], lambda s: s[0])]
  merge = lambda sub_results: sub_results[0]
  return solveDQ(
    state,
    base_cases,
    get_sub_problems,
    merge,
  )

if __name__ == "__main__":
  n = int(input())

  def isValid(n):
    def condition(mid):
      taken = 0
      nonlocal n
      base = n
      while(base):
        portion = base if base <= mid else mid
        taken += portion
        base -= portion
        base -= base // 10
      return 2 * taken >= n
    return condition

  print(binary_search_first_high(1, n, isValid(n)))

C++ Code:

#include <bits/stdc++.h>

#define F first
#define S second
#define endl "\n";
#define shekoo ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

const ll mod = 1e9+7;

bool check(ll mid, ll cur){
    ll sum = 0;
    ll n = cur;
    while(cur > 0){
        ll have = min(cur, mid);
        cur -= have;
        sum += have;
        cur -= (cur/10);
    }
    return (2*sum >= n);
}

int main() {
    shekoo
    ll n; cin >> n;
    ll st = 1, en = n, ans = 0;
    while(st <= en){
        ll mid = (st+en)/2;
        if(check(mid, n)){
            ans = mid;
            en = mid-1;
        }
        else{
            st = mid+1;
        }
    }
    cout << ans;
    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