답안 #620068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
620068 2022-08-02T21:08:36 Z amunduzbaev 사탕 분배 (IOI21_candies) C++17
29 / 100
529 ms 27256 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]]){
			assert(v[i] > 0);
			a[i] -= c[p[0]];
			if((int)ss.size() == 1 || v[*--ss.end()] < 0){
				tt.insert({a[i], i});
			} 
			ss.insert(i);
		} else if(a[i] < 0){ 
			assert(v[i] < 0);
			a[i] = 0 - a[i];
			if((int)ss.size() > 1 && v[(*--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);
	
	//~ for(auto x : ss) cout<<x<<" ";
	//~ cout<<"\n";
	//~ cout<<"->\n";
	//~ for(auto x : tt) cout<<x[0]<<" "<<x[1]<<"\n";
	//~ cout<<"\n";
	
	int j = 0;
	while(!tt.empty()){
		auto [x, i] = (*tt.begin()); 
		tt.erase(tt.begin());
		int k = *--ss.end();
		while(j < n && c[p[j]] < x + c[p[0]]){
			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)){
				a[k] += x;
				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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 304 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 201 ms 21560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 440 ms 23792 KB Output is correct
4 Correct 61 ms 3648 KB Output is correct
5 Correct 529 ms 26984 KB Output is correct
6 Correct 519 ms 27256 KB Output is correct
7 Correct 505 ms 27228 KB Output is correct
8 Correct 408 ms 27084 KB Output is correct
9 Correct 101 ms 11552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 304 KB Output isn't correct
3 Halted 0 ms 0 KB -