1765D - Watch the Videos - CodeForces Solution


binary search constructive algorithms two pointers *1700

Please click on ads to support us..

Python Code:

n, m = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
s = sum(a)
def possible(b, m):
    l = len(b)
    for i in range(l//2):
        if b[i]+b[l-1-i] > m:
            return False
    return True
minp = 1
maxp = n
while minp != maxp:
    midp = (minp+maxp+1)//2
    if possible(a[:midp], m):
        minp = midp
    else: maxp = midp-1
print(sum(a)+n+1-minp)

    

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <queue>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;
ll gcd (ll a, ll b) {return b==0 ? a : gcd(b, a%b);}
const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);

#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define srtrev(c) sort(all(c)); reverse(all(c))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define read(x) scanf("%d", &x)
#define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i])

#define pb push_back
#define mp make_pair
vi a ;
int64_t n , m ;
int64_t can ( int x ) {
      int l = x-1, r =n-1 ;
      while (l<r){
         if ( a[l]+a[r] >m){return 0 ; }
         else { l++, r--; }
      }
      if ( l==r ) {
        if ( a[l]+a[r+1]>m){return 0 ;}
        else {l++; r--; }
      }
      return 1;
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int ta;
    ta = 1 ;


    forn(ifa,0,ta) {

  cin>>n>>m ;

         a= vi(n) ;
         int64_t sum =0  ;
         forn ( i,0, n ) {cin >>a[i] ;sum+=a[i] ;}

      srtrev(a);
      int l= 1 , r = n;

      while (r-l>1){
          int mid = (l+r)/2;
          if (can ( mid ) ) r = mid ;
          else l = mid ;

      }

      if ( can(l )){ r = l ; }
      cout<<sum + r <<endl;

    }


}


Comments

Submit
0 Comments
More Questions

1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy