Submission #222706

# Submission time Handle Problem Language Result Execution time Memory
222706 2020-04-13T20:47:46 Z opukittpceno_hhr Candies (JOI18_candies) C++17
8 / 100
28 ms 640 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

#define int long long

const int N = 2001;
const int INF = 1e18 + 129;

int n;
int a[N];
int mapl[N];
int mapr[N];

int summ(int l, int r) {
	int ans = 0;
	for (int i = 0; i < r - l + 1; i++) {
		if (i & 1) {
			ans -= a[l + i];
		} else {
			// cerr << "add " << a[l + i] << endl;
			ans += a[l + i];
		}
	}
	return ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	set<pair<int, int>> seg;
	for (int i = 1; i <= n; i++) {
		mapl[i] = i;
		mapr[i] = i;
		seg.insert({i, i});
	}
	auto ers = [&](int l, int r) {
		// cerr << "hr " << l << ' ' << r << endl;
		auto it = seg.lower_bound({l, r});
		auto prv = it;
		if (it != seg.begin()) prv--;
		auto nxt = it;
		if (it != seg.end()) nxt++;
		if (seg.size() == 1) {
			seg.erase({l, r});
			return;
		}
		if (it == seg.begin()) {
			pair<int, int> we = *it;
			pair<int, int> next = *(nxt);
			seg.erase(we);
			seg.erase(next);
			// seg.insert({l, next.second});
			return;
		}
		if (it == --seg.end()) {
			pair<int, int> we = *it;
			pair<int, int> prev = *(prv);
			seg.erase(we);
			seg.erase(prev);
			// seg.insert({prev.first, r});
			return;
		}
		// cerr << "full erase " << l << ' ' << r << endl;
		// cerr << "was " << seg.size() << endl;
		pair<int, int> we = *it;
		pair<int, int> next = *(nxt);
		pair<int, int> prev = *(prv);
		// cerr << we.first << ' ' << we.second << endl;
		// cerr << prev.first << ' ' << prev.second << endl;
		// cerr << next.first << ' ' << next.second << endl;
		
		seg.erase(prev);
		seg.erase(we);
		seg.erase(next);
		if (prev.first)
		seg.insert({prev.first, next.second});
		// cerr << "now " << seg.size() << endl;
	};
	int curr = 0;
	vector<int> ans;
	while (!seg.empty()) {
		int mx = -INF;
		int lm = -1;
		int rm = -1;
		for (auto [l, r] : seg) {
			if (summ(l, r) > mx) {
				mx = summ(l, r);
				lm = l;
				rm = r;
			}
		}
		// cerr << "before " << seg.size() << ' ' << mx << endl;
		ers(lm, rm);
		curr += mx;
		ans.push_back(curr);
	}
	for (auto t : ans) {
		cout << t << ' ';
	}
	cout << endl;
	// cerr << summ(4, 4) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 512 KB Output is correct
2 Correct 17 ms 512 KB Output is correct
3 Correct 17 ms 512 KB Output is correct
4 Correct 16 ms 384 KB Output is correct
5 Correct 20 ms 640 KB Output is correct
6 Correct 17 ms 512 KB Output is correct
7 Correct 17 ms 512 KB Output is correct
8 Correct 28 ms 512 KB Output is correct
9 Correct 16 ms 512 KB Output is correct
10 Correct 15 ms 512 KB Output is correct
11 Correct 17 ms 512 KB Output is correct
12 Correct 17 ms 512 KB Output is correct
13 Correct 18 ms 512 KB Output is correct
14 Correct 16 ms 512 KB Output is correct
15 Correct 21 ms 512 KB Output is correct
16 Correct 16 ms 512 KB Output is correct
17 Correct 17 ms 512 KB Output is correct
18 Correct 17 ms 512 KB Output is correct
19 Correct 15 ms 512 KB Output is correct
20 Correct 16 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 512 KB Output is correct
2 Correct 17 ms 512 KB Output is correct
3 Correct 17 ms 512 KB Output is correct
4 Correct 16 ms 384 KB Output is correct
5 Correct 20 ms 640 KB Output is correct
6 Correct 17 ms 512 KB Output is correct
7 Correct 17 ms 512 KB Output is correct
8 Correct 28 ms 512 KB Output is correct
9 Correct 16 ms 512 KB Output is correct
10 Correct 15 ms 512 KB Output is correct
11 Correct 17 ms 512 KB Output is correct
12 Correct 17 ms 512 KB Output is correct
13 Correct 18 ms 512 KB Output is correct
14 Correct 16 ms 512 KB Output is correct
15 Correct 21 ms 512 KB Output is correct
16 Correct 16 ms 512 KB Output is correct
17 Correct 17 ms 512 KB Output is correct
18 Correct 17 ms 512 KB Output is correct
19 Correct 15 ms 512 KB Output is correct
20 Correct 16 ms 512 KB Output is correct
21 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
22 Halted 0 ms 0 KB -