1296D - Fight with Monsters - CodeForces Solution


greedy sortings *1500

Please click on ads to support us..

Python Code:

import sys, io, os
import math
import bisect
from sys import stdin,stdout
from math import gcd,floor,sqrt,log
from collections import defaultdict as dd
from bisect import bisect_left as bl,bisect_right as br

inp    =lambda: int(input())
strng  =lambda: input().strip()
jn     =lambda x,l: x.join(map(str,l))
strl   =lambda: list(input().strip())
mul    =lambda: map(int,input().strip().split())
mulf   =lambda: map(float,input().strip().split())
seq    =lambda: list(map(int,input().strip().split()))

ceil   =lambda x: int(x) if(x==int(x)) else int(x)+1
ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1

flush  =lambda: stdout.flush()
stdstr =lambda: stdin.readline()
stdint =lambda: int(stdin.readline())
stdpr  =lambda x: stdout.write(str(x) + "\n")
printls = lambda l: print(*l, sep=" ")
mod=1000000007


def solution(h,a,b,k):
    for i in range(len(h)):
        if h[i]%(a+b) != 0:
            h[i] = h[i]%(a+b)
        else:
            h[i] = a+b
    
    h.sort()

    points = 0
    for i in range(len(h)):
        if h[i] <= a:
            points += 1
        elif ceildiv(h[i], a) - 1 <= k:
            k = k - (ceildiv(h[i], a) - 1)
            points += 1
        else:
            return points
        
    return points
    

        
            

            
t = 1for i in range(t):
    n,a,b,k = mul()
    h = seq()
    ans = solution(h,a,b,k)
    print(ans)

C++ Code:

//Practice is the only shortcut to improve
#include<bits/stdc++.h>
using namespace std;

#define for0(i, n) for(int i=0; i<n; i++)
#define for1(i, n) for(int i=1; i<=n; i++)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define vi vector<int>
#define MOD 1000000007
#define nline "\n"
#define ff first
#define ss second
#define INF 1e18
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//graph
int findParent(int node,vector<int>& par){if(par[node]==node) return node; return par[node]=findParent(par[node],par);}
void Union(int u,int v,vector<int>& par,vector<int>& size){int pu=findParent(u,par);int pv=findParent(v,par);if(pu==pv) return;if(size[pu]>=size[pv]){par[pv]=pu;size[pu]++;}else{par[pu]=pv;size[pv]++;}} 

//segment tree -- Ranged Queries
void build_ranged_sum(vi& a, int v, int tl, int tr,vi& t)
{
    if (tl == tr)t[v] = a[tl];
    else {int tm = (tl + tr) / 2;
    build_ranged_sum(a, v*2, tl, tm,t);
    build_ranged_sum(a, v*2+1, tm+1, tr,t);
    t[v] = t[v*2] + t[v*2+1];
    }
}
int ranged_sum(int v, int tl, int tr, int l, int r,vi& t)
{
    if (l > r) return 0;
    if (l == tl && r == tr) {return t[v];}
    int tm = (tl + tr) / 2;
    return ranged_sum(v*2, tl, tm, l, min(r, tm),t)+ ranged_sum(v*2+1, tm+1, tr, max(l, tm+1), r,t);
}
void update(int v, int tl, int tr, int pos, int new_val,vi& t) 
{
    if (tl == tr) {t[v] = new_val;} 
    else {int tm = (tl + tr) / 2;
    if (pos <= tm) update(v*2, tl, tm, pos, new_val,t);
    else update(v*2+1, tm+1, tr, pos, new_val,t);
    t[v] = t[v*2] + t[v*2+1];
    }
}
/*---------------------------------------------------------------------------------------------------------------------------*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
/*---------------------------------------------------------------------------------------------------------------------------*/
/*  NOTES: 
    -> a << n = a * pow(2, n)
    -> a >> n = a / pow(2, n)
    -> if(n&1) odd else even
    -> min no of bits to change a to b ==> a ^ b 
        ex a = 11, b = 15, a = 1011, b = 1111, a^b = 0100
    -> Count the set bits ==> while(n){ cnt += (n&1), n = n >> 1}
    -> To remove the last set bit ==> n & (n-1) 
        ex a = 13, 1101, a & (a-1), 1101 & 1100 = 1100
    -> To extract the ith bit we use mask ==> x = n & (1 << i)
        ex n = 13, 1101, i = 2, so 1 << 2 = 0100, so we get 4
    -> Changing the ith bit ==> n | (1 << i)
        ex n = 13, i = 2, 1101 | (1<<2), 1101|0010 = 1111
    -> Clearing the ith bit ==> n & (~(1<<i)) 
        ex n = 13, i = 2, 1101 & (~(1<<i)), 1101 & (1011) = 1001
    -> To get first set bit y = x&(~(x-1))
    -> 
*/
void solve(){
    int n, k, a, b, cnt = 0; cin>>n>>a>>b>>k;
    vi vec(n), freq; 
    for0(i,n){
        cin>>vec[i]; 
        int x = vec[i]%(a+b);
        if(!x){
            freq.pb(b/a + (b%a != 0));
        }else if(x > a){
            int rem = x - a;
            freq.pb(rem/a + (rem%a != 0));
        }else cnt++;
    }
    sort(all(freq)); 
    for0(i, sz(freq)){
        if(freq[i] <= k) cnt++, k -= freq[i];
        else break;
    }
    cout<<cnt<<'\n';
}
signed main(){
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    fast;
    int t = 1; 
    // cin>>t;
    while(t--) solve();
    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