388A - Fox and Box Accumulation - CodeForces Solution


greedy sortings *1400

Please click on ads to support us..

Python Code:

from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from bisect import bisect_left,bisect_right

BUFSIZE = 8192


class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
	
	sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop("end", "\n"))
	if kwargs.pop("flush", False):
		file.flush()


if sys.version_info[0] < 3:
	sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
	sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

def inp():
    return(int(input()))
def inps():
    return input().strip()
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input().strip()
    return(list(s[:len(s)]))
def invr():
    return(map(int,input().split()))

from types import GeneratorType
def bootstrap(f,stack=[]):
    def wrappedfunc(*args,**kwargs):
        if stack:
            return f(*args,**kwargs)
        else:
            to=f(*args,**kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to=next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

n=inp()
l=inlt()
l.sort()
g=Counter(l)
d=defaultdict(lambda:0)
ans=0
for i in range(n):
    for j in range(n):
        if d[j]<=l[i]:
            d[j]+=1
            if d[j]==1:
                ans+=1
            break
print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define maxn          100007
#define pb            push_back
#define sz            size()
#define loop(i,n)     for(int i=0;i<n;i++)
#define loop1(i,n)    for(int i=1;i<=n;i++)
#define sc(n)         scanf("%d",&n)
#define sc2(n,m)      scanf("%lld %lld",&n,&m)
#define sc3(a,b,c)    scanf("%d %d %d",&a,&b,&c);
#define pf(n)         printf("%d",n)
#define pcase(a,b)    printf("Case %d: %lld\n",a,b);
#define ll            long long
#define ull           unsigned ll
#define all(v)        v.begin(),v.end()
#define pii           pair<int,int>
#define pll           pair<ll,ll>
#define pdd           pair<double,double>
#define vi            vector<int>
#define inf           1e18+4
#define ff            first
#define ss            second
#define ret           return
#define mod           1000000007
#define pi            acos(-1.0)
#define MP            make_pair
#define fileout       freopen("output.txt","w",stdout);
#define mem(a,n)      memset(a,n,sizeof a);
#define fastIO        ios_base::sync_with_stdio(false);cin.tie(NULL);
#define tc            int tc,caseNo=0;cin >> tc;while(tc--)
#define endl          '\n'

/*
int fx[] = {+1,+1,+2,+2,-1,-1,-2,-2};
int fy[] = {+2,-2,+1,-1,+2,-2,+1,-1};


*/



int main() 
{
    ll n,m,x,y,k,arr[2*maxn];
    cin>> n;
    vi v(101,0);
    for(int i=0;i<n;i++) {
        cin >> arr[i];
    }
    sort(arr,arr+n);
    int ans = 0;
    while(1) {
        int siz = 0;
        for(int i=0;i<n;i++) {
            if(arr[i]>=siz and !v[i]) {
                siz++;
                v[i] = 1;
            }
        }
        if(siz>0) ans++;
        else break;
    }
    cout << ans <<endl;
}


Comments

Submit
0 Comments
More Questions

Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array