This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define int long long
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 5e5+5;
vector<pll> ans[N];
vector<ll> aa[N];
int a[N];
void solve(){
int n, k, sum = 0, mx = 0, f = 1;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
sum += a[i];
mx = max(a[i], mx);
}
if(sum % k || sum < mx * k){
cout<<-1<<"\n";
return;
}
int cur = 0, cnt = 0, tmp = sum / k;
for(int i=0;i<n;i++){
if(cur + a[i] > tmp){
ans[cnt].pb({tmp - cur, i+1});
cnt++;
ans[cnt].pb({cur + a[i] - tmp, i+1});
cur = cur + a[i] - tmp;
}else if(cur + a[i] == tmp){
ans[cnt].pb({a[i], i+1});
cnt++;
cur = 0;
}else{
cur += a[i];
ans[cnt].pb({a[i], i+1});
}
}
cnt = 0;
while(f){
f = 0;
int mn = 9e18;
for(int i=0;i<k;i++)
mn = min(ans[i].back().ff, mn);
aa[cnt].pb(mn);
for(int i=0;i<k;i++){
pll tmp1 = ans[i].back();
ans[i].pop_back();
tmp1.ff -= mn;
aa[cnt].pb(tmp1.ss);
if(tmp1.ff > 0) ans[i].pb(tmp1);
}
f |= !ans[0].empty();
cnt++;
}
cout<<cnt<<"\n";
for(int i=0; i<cnt; i++){
for(int j=0; j<sz(aa[i]); j++){
cout<<aa[i][j]<<" ";
}cout<<"\n";
}
return;
}
main(){
fastios
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
nicegift.cpp:86:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main(){
| ^
# | 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... |