제출 #961258

#제출 시각아이디문제언어결과실행 시간메모리
961258pragmatistGift (IZhO18_nicegift)C++17
30 / 100
35 ms7184 KiB
#include<bits/stdc++.h>

using namespace std;

const int N = (int)1e6+7;

int n, k, a[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	cin >> n >> k;
	long long tot = 0;
	int mx = 0;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		tot += a[i];
		mx = max(mx, a[i]);
	}    
	if(tot%k) {
		cout << "-1\n";
		return 0;
	}
	if(mx>tot/k) {
		cout << "-1\n";
		return 0;
	}
	set<pair<int, int> > s;
	for(int i = 1; i <= n; ++i) {
		s.insert({a[i], i});
	}
	vector<vector<long long> > ans;
	while(!s.empty()) {
		vector<int> cur;       
		for(int j = 0; j < k; ++j) {
			auto it = --s.end();
			cur.push_back(it->second);
			s.erase(it);
		}
		int can = 1;
		if(!s.empty()) {
			can = (--s.end())->first;
		}
		can = a[cur.back()]-can+1;
		vector<long long> v={can};
		for(auto id : cur) {
			assert(a[id]>=can);
			v.push_back(id);
		}
		ans.push_back(v);
		for(auto id : cur) {
			a[id] -= can;
			if(a[id]>0) {
				s.insert({a[id], id});
			}
		}
	}
	cout << (int)ans.size() << "\n";
	for(auto x : ans) {
		for(auto y : x) {
			cout << y << ' ';
		}
		cout << "\n";
	}
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...