Submission #620044

# Submission time Handle Problem Language Result Execution time Memory
620044 2022-08-02T20:09:08 Z amunduzbaev Distributing Candies (IOI21_candies) C++17
0 / 100
595 ms 33232 KB
#include "candies.h"

#include "bits/stdc++.h"
using namespace std;

#ifndef EVAL
#include "grader.cpp"
#endif

typedef long long ll;
#define ar array

const ll INF = 1e18;
const int inf = 1e9 + 5;
const int N = 2e5 + 5;

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
	int n = c.size();
	int q = l.size();
	
	vector<int> p(n);
	iota(p.begin(), p.end(), 0);
	sort(p.begin(), p.end(), [&](int i, int j){
		return c[i] < c[j];
	});
	
	set<int> ss;
	set<ar<ll, 2>> tt;
	ss.insert(-1);
	vector<ll> a(q);
	for(int i=0;i<q;i++){
		a[i] = min(a[i], (ll)c[p[0]]);
		a[i] = max(a[i], 0ll);
		a[i] += v[i];
		if(i + 1 < q) a[i + 1] = a[i];
		if(a[i] > c[p[0]]){
			a[i] -= c[p[0]];
			if(!ss.empty() || a[*--ss.end()] < 0){
				tt.insert({a[i], i});
			} 
			ss.insert(i);
		} else if(a[i] < 0){
			a[i] = 0 - a[i];
			if(!ss.empty() && a[(*--ss.end())] >= 0){
				tt.insert({a[i], i});
			}
			ss.insert(i);
		}
	}
	
	vector<ll> suff(q + 1);
	for(int i=q-1;~i;i--){
		suff[i] = suff[i + 1] + v[i];
	}
	
	vector<int> res(n);
	
	int j = 0;
	while(!tt.empty()){
		auto [x, i] = (*tt.begin()); 
		tt.erase(tt.begin());
		while(j < n && c[p[j]] < x + c[p[0]]){
			int k = *--ss.end();
			res[p[j]] = suff[k + 1] + (~k && v[k] > 0 ? c[p[j]] : 0);
			j++;
		}
		
		assert(ss.count(i));
		ss.erase(i);
		auto it = ss.lower_bound(i);
		if(it != ss.end()){
			int k = *it;
			if((v[k] > 0) == (v[i] > 0)){
				tt.insert({a[k], k});
			} else {
				//~ assert(tt.count({a[k], k}));
				tt.erase({a[k], k});
				a[k] -= x;
			}
		}
	}
	
	while(j < n){
		int k = *--ss.end();
		res[p[j]] = suff[k + 1] + (~k && v[k] > 0 ? c[p[j]] : 0);
		j++;
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 595 ms 33232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -