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 f first
#define s second
#define int long long
#define pb push_back
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int n,k,a[N],mx,cur,sum,le,ri,mid,sum1,mx1,mn,anss,cnt;
vector < pii > v;
vector <int> ans[N];
vector < pii > v1;
int pas[N];
set <pii > :: iterator it;
set < pii > ms;
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
for (int i = 1; i <= n; i++) {
cin>>a[i];
mx = max(mx,a[i]);
sum += a[i];
ms.insert({a[i],i});
}
if (mx > sum / k || sum % k) {
cout<<"-1"<<endl;
return 0;
}
int ss = 0;
sum1 = sum;
while (true) {
v.clear();v1.clear();
mx = mx1 = 0;
if (ms.size() == 0) break;
mn = 1e9;
it = ms.end();
cnt = 0;
while (true) {
it--;
cnt++;
if (cnt <= k) {
mx = max(mx,(*it).f);
mn = min(mn,(*it).f);
}
if (cnt == k + 1) {
mx1 = (*it).f;
break;
}
if (it == ms.begin()) break;
}
le = 1;
ri = 1e18;
while(le <= ri) {
mid = (le + ri) / 2;
if (mn < mid || max(mx1,mx-mid)>(sum1 - k*mid)/k) {
ri = mid - 1;
} else {
anss = mid;
le = mid + 1;
}
}
cur++;
pas[cur] = anss;
for (int i = 0; i < k; i++) {
it = ms.end();
it--;
int x = (*it).f; int y = (*it).s;
a[y] -= anss;sum1 -= anss;
v1.pb({x - anss, y});
ans[cur].pb(y);
ms.erase(it);
}
for (int i = 0; i < v1.size(); i++) {
if (v1[i].f != 0)
ms.insert({v1[i].f,v1[i].s});
}
}
cout<<cur<<"\n";
for (int i = 1; i <= cur; i++) {
cout<<pas[i]<<" ";
for (int j = 0; j < ans[i].size(); j++) {
cout<<ans[i][j]<<" ";
}cout<<"\n";
}
}
Compilation message (stderr)
nicegift.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main() {
| ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:73:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i = 0; i < v1.size(); i++) {
| ~~^~~~~~~~~~~
nicegift.cpp:81:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j = 0; j < ans[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
nicegift.cpp:29:9: warning: unused variable 'ss' [-Wunused-variable]
29 | int ss = 0;
| ^~
# | 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... |