# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112683 | dosts | Gift (IZhO18_nicegift) | C++17 | 1959 ms | 118156 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 5e2+1,inf = 1e16,MOD = 1e9+7;
void solve() {
int n,k;
cin >> n >> k;
int s = 0;
set<pii,greater<pii>> ms;
vi a(n+1);
for (int i=1;i<=n;i++) {
cin >> a[i];
s+=a[i];
ms.insert({a[i],i});
}
if (s%k != 0 || ms.rbegin()->ff > s/k) {
cout << -1 << endl;
return;
}
vector<pair<int,vi>> ops;
int sm = s/k;
vi vv(k,0);
while (!ms.empty()) {
if (ms.size() == k) {
if(ms.begin()->ff != ms.rbegin()->ff) {
cout << -1 << endl;
return;
}
}
auto itt = ms.begin();
int ctr = 0;
for (;ctr != k;++itt) {
vv[ctr] = (itt->ss);
ctr++;
}
int knxt = next(ms.begin(),k-1)->ff;
int subt = min(knxt,sm-((ms.size() > k) ? (next(ms.begin(),k)->ff) : 0ll));
sm-=subt;
for (auto it : vv) {
ms.erase({a[it],it});
if (a[it]-subt > 0) ms.insert({a[it]-subt,it});
//else assert(a[it] == subt);
a[it]-=subt;
}
ops.push_back({subt,vv});
}
cout << ops.size() << '\n';
for (auto it : ops) {
cout << it.ff << " ";
for (auto itt : it.ss) cout << itt << " ";
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |