Submission #511144

# Submission time Handle Problem Language Result Execution time Memory
511144 2022-01-15T09:16:08 Z Mazaalai Gift (IZhO18_nicegift) C++17
0 / 100
2 ms 588 KB
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LLA(x) x.rbegin(),x.rend()
#define print(x) for(auto el:x)cout<<el<<' ';cout<<"\n";
#define printPII(x) for(auto [a,b]:x)cout<<a<<","<<b<<' ';cout<<'\n';
using namespace std;
using ll = long long;
using PII = pair <ll, ll>;
ll n, k, onePick = 1;
bool impossible;
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	cin >> n >> k;
	vector <vector <ll> > ans;
	set <PII> bfs;
	ll sum = 0, val, id, dbg = 0;
	for (int i = 1; i <= n; i++) {
		ll x; cin >> x;
		sum += x;
		bfs.insert({x, i});
	}
	bfs.insert({0, 0});
	ll packs = sum / k;
	if (sum % k != 0 || bfs.rbegin()->ff > packs) {
		cout << "-1\n";
		return 0;
	}
	while(packs > 0) {
		vector <PII> add;
		for (int i = 0; i < k; i++) {
			auto it = --bfs.end();
			tie(val, id) = *it;
			bfs.erase(it);
			add.pb({val, id});
		}
		ll pick = min(add.back().ff, packs-((--bfs.end())->ff));
		packs -= pick;
		vector <ll> cur(1, pick);
		for (int i = 0; i < k; i++) {
			tie(val, id) = add.back();
			add.pop_back();
			val -= pick;
			cur.pb(id);
			bfs.insert({val, id});
		}
		ans.pb(cur);
	}
	cout << ans.size() << '\n';
	for (auto el : ans) {print(el);}
}

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:22:23: warning: unused variable 'dbg' [-Wunused-variable]
   22 |  ll sum = 0, val, id, dbg = 0;
      |                       ^~~
nicegift.cpp:17:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |  freopen("in.txt", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:18:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |  freopen("out.txt", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 532 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 532 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 532 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 588 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 532 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -