Submission #447162

# Submission time Handle Problem Language Result Execution time Memory
447162 2021-07-24T19:50:01 Z LucaDantas Distributing Candies (IOI21_candies) C++17
29 / 100
196 ms 14188 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
 
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const set<A> &s) { os << '{'; string sep = ""; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << '{'; string sep = ""; for (const auto &x : m) os << sep << x.first << " -> " << x.second, sep = ", "; return os << '}'; }
 
void debug() { cerr << endl; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...);}
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)
 
constexpr int inf = 0x3f3f3f3f;
 
// subtask l = 0, r = n-1 for all events
 
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = (int)(c.size());
	vector<int> ans(n);
	vector<pair<int,int>> td;
	for(int i = 0; i < n; i++)
		td.push_back({c[i], i});
	sort(td.begin(), td.end(), greater<pair<int,int>>());
	vector<int> seq;
	int add = 0, m = (int)(v.size());
	for(int i = 0; i < m; i++) {
		if((v[i] > 0) == (add > 0) || !add) add += v[i], add = max(-inf, min(add, inf));
		else seq.push_back(max(0, min(inf, add + (seq.size() ? seq.back() : 0)))), add = v[i];
	}
	if(add) seq.push_back(max(0, min(inf, add + (seq.size() ? seq.back() : 0))));
	
	vector<pair<int,int>> gld;
	int mn = inf;
	for(int i = (int)(seq.size())-1; i >= 0; i--) {
		mn = min(mn, seq[i]);
		gld.push_back({seq[i], seq[i] - mn}); // intervalo que fica ativo
	}
	sort(gld.begin(), gld.end());
	reverse(gld.begin(), gld.end());

	/* puts("SEQ");
	for(int i = 0; i < (int)seq.size(); i++)
		printf("%d ", seq[i]);
	puts("");

	puts("GLD");
	for(int i = 0; i < (int)seq.size(); i++)
		printf("%d %d\n", gld[i].first, gld[i].second); */

	// exit(0);
	
	int acabado = 0;
	set<pair<int,int>> atv, maior;
 
	for(int i = 0, j = 0; i < n; i++) {
		for(; j < (int)(gld.size()) && gld[j].first >= td[i].first; j++)
			atv.insert({gld[j].second, gld[j].first}), maior.insert(gld[j]);
		while(atv.size() && atv.rbegin()->first >= td[i].first) {
			acabado = max(acabado, atv.rbegin()->second - atv.rbegin()->first);
			maior.erase({atv.rbegin()->second, atv.rbegin()->first});
			atv.erase(*atv.rbegin());
		}

		/* fprintf(stderr, "acabado %d\n", acabado);
		db(td[i]);
		db(atv, maior); */

		ans[td[i].second] = seq.back() - acabado;
		if(maior.size()) ans[td[i].second] = min(ans[td[i].second], seq.back() - (maior.rbegin()->first - td[i].first));
	}
 
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 165 ms 9512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 110 ms 6492 KB Output is correct
4 Correct 81 ms 4792 KB Output is correct
5 Correct 188 ms 12496 KB Output is correct
6 Correct 196 ms 11732 KB Output is correct
7 Correct 174 ms 11044 KB Output is correct
8 Correct 185 ms 14188 KB Output is correct
9 Correct 128 ms 10920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -