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>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll n,k,a[2000005],s,las,p,cur,maxi,l,r,mid,can,ans1;
set<pair<ll,int> >st;
vector<vector<ll> >ans;
ll g[4000005],sz=0,wsz;
int main(){
scanf("%lld%lld",&n,&k);
bool ok = 1;
for(int i=1; i<=n; i++){
scanf("%lld",&a[i]);
s += a[i];
st.insert(mp(-a[i] , i));
}
if(s % k){
cout << "-1";
return 0;
}
ll raod = s / k;
ll sum = s;
ll ra = 0;
while(sum){
if((int)st.size() < k){
cout << "-1";
return 0;
}
p = 0,cur = 0;
maxi = -(*st.begin()).f;
g[sz] = -1;
sz++;
wsz = sz;
for(set<pair<ll,int> >::iterator it = st.begin(); it != st.end(); it++){
p++;
if(p > k){
cur = (*it).s;
break;
}
las = (*it).s;
g[sz] = ((*it).s);
sz++;
}
ll x1 = a[las];
if(k > 1)x1 = (sum - maxi) / (k - 1);
can = (sum - a[cur] * k) / k;
if(can > x1)can = x1;
if(can > a[las])can = a[las];
if(can <= 0){
cout << "-1";
return 0;
}
sum -= can * k;
ra += k;
p = 0;
ans1++;
g[wsz - 1] = can;
for(int i=wsz; i<sz; i++){
st.erase(mp(-a[g[i]] , g[i]));
a[g[i]]-=can;
if(a[g[i]])st.insert(mp(-a[g[i]] , g[i]));
}
}
printf("%lld ",ans1);
for(int i=0; i<sz; i++)
printf("%lld ",g[i]);
return 0;
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:14:8: warning: unused variable 'ok' [-Wunused-variable]
bool ok = 1;
^~
nicegift.cpp:26:8: warning: unused variable 'raod' [-Wunused-variable]
ll raod = s / k;
^~~~
nicegift.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&n,&k);
~~~~~^~~~~~~~~~~~~~~~~~
nicegift.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&a[i]);
~~~~~^~~~~~~~~~~~~~
# | 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... |