This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
//#include "teams.h"
const ll sizz = 500005;
vector<ii>arr;
ll R[sizz];
ll n;
vector<ll>seg[4 * sizz];
void build(ll s,ll e,ll idx){
if(s == e){
seg[idx].pb(arr[s-1].S);
return;
}
ll mid = (s + e) / 2;
build(s,mid,idx * 2);
build(mid+1,e,idx * 2 + 1);
ll p1 = 0,p2 = 0;
while(p1 < seg[idx * 2].size() || p2 < seg[idx * 2 + 1].size()){
if(p1 == seg[idx * 2].size()){
seg[idx].pb(seg[idx * 2 + 1][p2]);
p2++;
}
else if(p2 == seg[idx * 2 + 1].size()){
seg[idx].pb(seg[idx * 2][p1]);
p1++;
}
else{
if(seg[idx * 2][p1] < seg[idx * 2 + 1][p2]){
seg[idx].pb(seg[idx * 2][p1]);
p1++;
}
else{
seg[idx].pb(seg[idx * 2 + 1][p2]);
p2++;
}
}
}
}
ll query(ll s,ll e,ll qs,ll qe,ll idx,ll k){
if(qs <= s && e <= qe){
return upper_bound(all(seg[idx]),k) - seg[idx].begin();
}
if(s > qe || qs > e){
return 0;
}
ll mid = (s + e) / 2;
ll a = query(s,mid,qs,qe,idx * 2,k);
ll b = query(mid+1,e,qs,qe,idx * 2 + 1,k);
return a + b;
}
void init(int N, int A[], int B[]){
n = N;
f(i,0,n){
arr.pb(ii(A[i],B[i]));
}
sort(all(arr));
ll pos = 1;
for(auto x:arr){
R[x.F] = pos;
pos++;
}
f(i,1,n+1){
R[i] = max(R[i],R[i-1]);
}
build(1,n,1);
}
int can(int M, int K[]) {
ll sum = 0;
vector<ii>ex;
sort(K,K+M);
f(i,0,M){
sum += K[i];
if(i > 0 && K[i] == K[i-1]){
ex.back().S++;
}
else{
ex.pb(ii(K[i],1));
}
}
if(sum > n){
return 0;
}
ll siz = ex.size();
ll posa[siz] = {0};
ll l = 1;
ll j = 0;
for(auto x:ex){
ll r = R[x.F];
ll ev = query(1,n,l,r,1,ex[max(0,j-1)].F - 1);
f(i,j-1,siz){
ll num = INF;
if(i != (siz - 1)){
num = ex[i+1].F - 1;
}
ll v = query(1,n,l,r,1,num);
posa[i] += v - ev;
ev = v;
}
ll em = x.S * x.F;
f(i,j,siz){
ll E = min(posa[i],em);
em -= E;
posa[i] -= E;
if(em == 0){
break;
}
}
if(em > 0){
return 0;
}
l = r + 1;
j++;
}
return 1;
}
/*
int main() {
int N;
cin>>N;
int *A = (int*)malloc(sizeof(int)*(unsigned int)N);
int *B = (int*)malloc(sizeof(int)*(unsigned int)N);
for (int i = 0; i < N; ++i) {
cin>>A[i]>>B[i];
}
init(N, A, B);
int Q;
cin>>Q;
for (int i = 0; i < Q; ++i) {
int M;
cin>>M;
int *K = (int*)malloc(sizeof(int)*(unsigned int)M);
for (int j = 0; j < M; ++j) {
cin>>K[j];
}
printf("%d\n", can(M, K));
}
return 0;
}
*/
/*
4
2 4
1 2
2 3
2 3
2
2 1 3
2 1 1
*/
Compilation message (stderr)
teams.cpp: In function 'void build(ll, ll, ll)':
teams.cpp:41:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while(p1 < seg[idx * 2].size() || p2 < seg[idx * 2 + 1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~
teams.cpp:41:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while(p1 < seg[idx * 2].size() || p2 < seg[idx * 2 + 1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
teams.cpp:42:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if(p1 == seg[idx * 2].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~
teams.cpp:46:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | else if(p2 == seg[idx * 2 + 1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
teams.cpp: In function 'll query(ll, ll, ll, ll, ll, ll)':
teams.cpp:64:45: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'll' {aka 'int'} may change value [-Wconversion]
64 | return upper_bound(all(seg[idx]),k) - seg[idx].begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:106:18: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'll' {aka 'int'} may change value [-Wconversion]
106 | ll siz = ex.size();
| ~~~~~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |