This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#include "robots.h"
const int MAX_A = 5e4 + 5;
const int MAX_N = 1e6 + 5;
int N;  // number of toys
int A, B;   // count capped by weight, by size
vi X, Y;
vector<ii> toys;	// {size, weight}
bool check(int D) { // true if can clean in D days
//	cout<<"at "<<D<<endl;
	if (D * (A + B) < N)
		return false;
	vi alls;	// all capped by size
	int ctr = 0;
	for (int i = B - 1; i >= 0; i--) {
		for (int j = 0; j < D; j++) {
			alls.pb(Y[i]);
			ctr++;
			if (ctr == N)
				break;
		}
		if (ctr == N)
			break;
	}
	reverse(alls.begin(), alls.end());
/*	cout<<"alls: ";
	for (int x : alls)
		cout<<x<<" ";
	cout<<endl;*/
	priority_queue<int, vector<int>> pq;	// existing weights
	// remove heaviest posssible
	multiset<int> allw;
	for (ii p : toys)
		allw.insert(p.se);
	int idx = 0;	// next index to add
	for (int x : alls) {
//		cout<<"at "<<x<<endl;
		while (idx < N) {
			if (toys[idx].fi <= x) {
				pq.push(toys[idx].se);
				idx++;
			}
			else {
				break;
			}
		}
//		cout<<"working "<<pq.size()<<endl;
		// remove the heaviest
		if (!pq.empty()) {
			int x = pq.top();
			pq.pop();
//			cout<<"removing "<<x<<endl;
			allw.erase(allw.find(x));
		}
	}
	// check if majorizes
	vi remw;	// remaining weights
	for (int x : allw)
		remw.pb(x);
/*	cout<<"remw: ";
	for (int x : remw)
		cout<<x<<" ";
	cout<<endl;*/
	ctr = (int)(remw.size()) - 1;
	for (int i = A - 1; i >= 0; i--) {
		for (int j = 0; j < D; j++) {
			if (X[i] < remw[ctr])
				return false;
			ctr--;
			if (ctr == -1)
				break;
		}
		if (ctr == -1)
			break;
	}
	if (ctr != -1)
		return false;
	return true;
}
// if need to speed up, change multiset to priority queue (?)
int putaway(int a, int b, int t, int c[], int d[], int w[], int s[]) {
	A = a; B = b; N = t;
	sort(c, c + A);
	sort(d, d + B);
	for (int i = 0; i < A; i++) {
		X.pb(c[i] - 1);
	}
	for (int i = 0; i < B; i++) {
		Y.pb(d[i] - 1);
	}
	// sort toys by increasing size
	for (int i = 0; i < N; i++) {
		toys.pb({s[i], w[i]});
	}
	sort(toys.begin(), toys.end());
//	cout<<check(3)<<endl;
	int low = 0;
	int high = N;   // at most N minutes if possible
	int ans = -1;   // min number of days D to clean all
	while (low <= high) {   // false, false, false, true, true, true
		int mid = (low + high) / 2;
		if (check(mid)) {
			high = mid - 1;
			ans = mid;
		}
		else {
			low = mid + 1;
		}
	}
	return ans;
//	return 0;
}
| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |