이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 > 1500000){
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;
}
컴파일 시 표준 에러 (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... |