답안 #511100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
511100 2022-01-15T06:04:26 Z Mazaalai Gift (IZhO18_nicegift) C++17
0 / 100
3 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;
	vector <ll> vals, vals1;
	for (int i = 1; i <= n; i++) {
		ll x; cin >> x;
		sum += x;
		vals.pb(x);
		bfs.insert({x, i});
	}
	sort(LLA(vals));
	if (sum % k != 0) {
		cout << "-1\n";
		return 0;
	}
	if (vals[0] == vals.back()) {
		ll cycleLen = k * n / gcd(k, n); // 4, 6, 2-> 12
		ll oneCycleContribution = cycleLen / n; // 
		onePick = vals[0] / oneCycleContribution;
		for (auto& el : vals) el /= onePick;
		sum /= onePick;
	}
	while(!impossible && sum > 0) {
		vector <ll> pick(1, onePick);
		vector <PII> add;
		for (int i = 0; i < k; i++) {
			auto it = --bfs.end();
			PII cur = *it;
			bfs.erase(it);
			pick.pb(cur.ss);
			cur.ff--;
			sum--;
			if (cur.ff < 0) {
				impossible = 1;
				break;
			}
			add.pb(cur);
		}
		ans.pb(pick);
		for (auto el : add) bfs.insert(el);
	}
	// int dbg = 0;
	if (impossible) {
		cout << "-1\n";
		return 0;
	}
	cout << ans.size() << '\n';
	for (auto el : ans) {
		print(el);
	}
}

Compilation message

nicegift.cpp: In function 'int main()':
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);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -