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>
#pragma GCC optimize "-O3"
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#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,x1;
set<pair<ll,int> >st;
vector<vector<ll> >ans;
ll g[4000005],sz=0,wsz;
set<pair<ll,int> >::iterator it;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
bool ok = 1;
for(int i=1; i<=n; i++){
cin >> a[i];
s += a[i];
st.insert(mp(-a[i] , i));
}
if(s % k){
cout << "-1";
return 0;
}
ll sum = s;
while(sum){
if(sz > 2000000){
cout << "-1";
return 0;
}
if((int)st.size() < k){
cout << "-1";
return 0;
}
p = 0,cur = 0;
maxi = -(*st.begin()).f;
g[sz] = -1;
sz++;
wsz = sz;
for(it = st.begin(); it != st.end(); it++){
p++;
if(p > k){
cur = (*it).s;
break;
}
las = (*it).s;
g[sz] = (*it).s;
sz++;
}
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];
sum -= can * k;
ans1++;
g[wsz - 1] = can;
for(int i=wsz; i<sz; i++){
st.erase(st.find(mp(-a[g[i]] , g[i])));
a[g[i]]-=can;
if(a[g[i]])st.insert(mp(-a[g[i]] , g[i]));
}
}
cout << ans1 << " ";
for(int i=0; i<sz; i++)
cout << g[i] << " ";
return 0;
}
Compilation message (stderr)
nicegift.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
nicegift.cpp: In function 'int main()':
nicegift.cpp:21:8: warning: unused variable 'ok' [-Wunused-variable]
bool ok = 1;
^~
# | 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... |