Submission #495328

# Submission time Handle Problem Language Result Execution time Memory
495328 2021-12-18T12:47:51 Z Ziel Gift (IZhO18_nicegift) C++17
7 / 100
2000 ms 398456 KB
/**
 * LES GREATEABLES BRO TEAM
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");

#define int ll

const int N = 2e6 + 4;

ll n, k, a[N];

void solve() {
    cin >> n >> k;
    ll sum = 0, mx = 0;
    for (int i = 1; i <= n; i++) {
    	cin >> a[i];
    	mx = max(mx, a[i]);
    	sum += a[i];
    }
	if (sum % k != 0 || mx > sum / k) {
		cout << -1;
		return;
	}
	sum /= k;
	ll cur_sum = 0;
	set<pair<int, int>> s[k + 1];
	int id = 1;
	for (int i = 1; i <= n; i++) {
		if (cur_sum + a[i] <= sum) {
			cur_sum += a[i];
			s[id].insert({a[i], i});
			if (cur_sum == sum) {
				id++;
				cur_sum = 0;
			}
		} else if (cur_sum + a[i] > sum) {
			int y = cur_sum + a[i] - sum, x = a[i] - y;
			s[id].insert({x, i});
			id++;
			s[id].insert({y, i});
			cur_sum = 0;
		}
	}
	vector<vector<int>> ans;
	while (true) {
		int mx = 1000000000;
		for (int i = 1; i <= k; i++) {
			mx = min(mx, s[i].begin()->first);
		}
		if (!mx)
			break;
		vector<int> cur;
		cur.push_back(mx);
		for (int i = 1; i <= k; i++) {
			cur.push_back(s[i].begin()->second);
			int v = s[i].begin()->first, se = s[i].begin()->second;
			s[i].erase(s[i].begin());
			v -= mx;
			if (v)
				s[i].insert({v, se});
		}
		ans.push_back(cur);
		for (int i = 1; i <= k; i++) {
			if (!sz(s[i])) {
				break;
			}
		}
	}
	cout << sz(ans) << '\n';
	for (auto x: ans) {
		for (auto y: x)
			cout << y << ' ';
		cout << '\n';
	}
}

signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}

void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message

nicegift.cpp: In function 'void setIO(const string&)':
nicegift.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 316 KB n=4
5 Correct 0 ms 316 KB n=4
6 Correct 0 ms 204 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 316 KB n=4
5 Correct 0 ms 316 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 332 KB n=8
9 Correct 0 ms 332 KB n=14
10 Incorrect 0 ms 332 KB Same heap occurs twice
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 316 KB n=4
5 Correct 0 ms 316 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 332 KB n=8
9 Correct 0 ms 332 KB n=14
10 Incorrect 0 ms 332 KB Same heap occurs twice
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2103 ms 398456 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 316 KB n=4
5 Correct 0 ms 316 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 332 KB n=8
9 Correct 0 ms 332 KB n=14
10 Incorrect 0 ms 332 KB Same heap occurs twice
11 Halted 0 ms 0 KB -