# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
175589 | juggernaut | Gift (IZhO18_nicegift) | C++14 | 759 ms | 134876 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.
//Just try and the idea will come
#include<bits/stdc++.h>
#define int long long int
using namespace std;
int n,k,sum,mx,i,a[1000001],mn,sz,res,cnt[1000001],j,tmp;
vector<pair<int,int>>vec[1000001];
vector<int>ans[1000001];
bool flag;
main(){
scanf("%lld%lld",&n,&k);
for(i=1;i<=n;i++)scanf("%lld",&a[i]),mx=max(mx,a[i]),sum+=a[i];
if(sum%k!=0||mx>sum/k){
puts("-1");
return 0;
}
tmp=0;
for(i=1;i<=n;i++){
tmp+=a[i];
if(tmp>=sum/k){
vec[sz++].push_back({a[i]-tmp+sum/k,i});
tmp-=sum/k;
if(tmp)vec[sz].push_back({tmp,i});
}else vec[sz].push_back({a[i],i});
}
while(!flag){
mn=1e18;
for(i=0;i<sz;i++)mn=min(mn,vec[i].back().first);
cnt[res]=mn;
flag=0;
for(i=0;i<sz;i++){
ans[res].push_back(vec[i].back().second);
if(vec[i].back().first==mn)vec[i].pop_back();
else vec[i].back().first-=mn;
flag|=vec[i].empty();
}
if(flag)for(i=0;i<sz;i++)if(!vec[i].empty())runtime_error(0);
res++;
}
printf("%lld\n",res);
for(i=0;i<res;i++){
printf("%lld",cnt[i]);
for(j=0;j<k;j++)printf(" %lld",ans[i][j]);
puts("");
}
}
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... |