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;
typedef pair<int64_t,int64_t> ii;
int64_t n,k,top=0,a[1000005],rs[3000005];
vector<int64_t> g[3000005];
priority_queue<ii> Q;
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
//freopen("TEST.OUT","w",stdout);
cin>>n>>k;
int64_t sum=0,maxn=0;
for(int64_t i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
maxn=max(maxn,a[i]);
Q.push(ii(a[i],i));
}
if(sum%k!=0||maxn>sum/k)
{
cout<<-1;
return 0;
}
int64_t ave=sum/k;
while(Q.size()>0)
{
vector<int64_t> TMP;
for(int64_t i=1;i<=k;i++)
{
int64_t u=Q.top().second;
Q.pop();
TMP.push_back(u);
}
int64_t tmp=0;
if(Q.size()>0)
tmp=Q.top().first;
int64_t lim=min(a[TMP.back()],ave-tmp);
ave-=lim;
rs[++top]=lim;
g[top]=TMP;
for(int64_t i=0;i<TMP.size();i++)
{
a[TMP[i]]-=lim;
if(a[TMP[i]]>0)
Q.push(ii(a[TMP[i]],TMP[i]));
}
}
cout<<top<<"\n";
for(int64_t i=1;i<=top;i++)
{
cout<<rs[i]<<' ';
for(int64_t j=0;j<g[i].size();j++)
cout<<g[i][j]<<' ';
cout<<"\n";
}
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:45:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int64_t i=0;i<TMP.size();i++)
~^~~~~~~~~~~
nicegift.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int64_t j=0;j<g[i].size();j++)
~^~~~~~~~~~~~
# | 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... |