Submission #495336

# Submission time Handle Problem Language Result Execution time Memory
495336 2021-12-18T13:18:54 Z Ziel Gift (IZhO18_nicegift) C++17
0 / 100
1298 ms 524292 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;
	deque<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].push_back({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].push_back({x, i});
			id++;
			s[id].push_back({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].front().first);
		}
		if (!mx)
			break;
		vector<int> cur;
		cur.push_back(mx);
		for (int i = 1; i <= k; i++) {
			cur.push_back(s[i].front().second);
			s[i].front().first -= mx;
			if (!s[i].front().first) {
				s[i].pop_front();
			}
		}
		ans.push_back(cur);
		for (int i = 1; i <= k; i++) {
			if (!sz(s[i])) {
				break;
			}
		}
	}
	for (int i = 1; i <= k; i++) {
		if (!sz(s[i])) {
			cout << -1;
			return;
		}
	}
	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:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1298 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -