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 pb push_back
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(data) data.begin() , data.end()
#define endl '\n'
//freopen("nenokku_easy.in", "r", stdin);
//freopen("nenokku_easy.out", "w", stdout);
#define int long long
#define pii pair < int, int >
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int n, k, a[N], uk[2021];
vector < pii > m[2021];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int sum = 0, mx = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
mx = max(mx, a[i]);
}
if(sum % k != 0 || mx * k > sum) {
cout << -1;
return 0;
}
sum /= k;
int cur = 0, last = 1;
for(int i = 1; i <= n; i++) {
if(cur + a[i] == sum) {
m[last].pb({a[i], i});
cur = 0;
last++;
} else if(cur + a[i] > sum) {
m[last].pb({sum - cur, i});
last++;
m[last].pb({a[i] - sum + cur, i});
cur = a[i] - sum + cur;
} else {
m[last].pb({a[i], i});
cur += a[i];
}
}
if(m[last].empty()) last--;
srand(time(0));
random_shuffle(a + 1, a + n + 1);
vector < pair < int, vector < int > > > ans;
while(true) {
bool ok = 0;
ll mn = 1e18;
for(int block = 1; block <= last; block++) {
int l = uk[block];
if(l == m[block].size()) {
ok = 1;
break;
}
mn = min(m[block][l].first, mn);
}
if(ok) break;
vector < int > v;
for(int block = 1; block <= last; block++) {
int l = uk[block];
v.pb(m[block][l].second);
m[block][l].first -= mn;
if(m[block][l].first == 0) uk[block]++;
}
ans.pb({mn, v});
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++) {
cout << ans[i].first;
vector < int > v = ans[i].second;
for(int j = 0; j < v.size(); j++) {
cout << " " << v[j];
}
cout << endl;
}
}
/*
5
2 8 5 1 10
5
2 3 5 3
2 5 3 3
1 4 4
2 3 4 2
1 1 2
*/
Compilation message (stderr)
nicegift.cpp:19:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
19 | main() {
| ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:60:18: 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]
60 | if(l == m[block].size()) {
| ~~^~~~~~~~~~~~~~~~~~
nicegift.cpp:77:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::vector<long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0; i < ans.size(); i++) {
| ~~^~~~~~~~~~~~
nicegift.cpp:80:26: 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]
80 | for(int j = 0; j < v.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... |