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>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
for (auto a : x) cout << x << ' ';\
cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 2000000;
lli a[N];
int n, k;
struct op {
vector <int> ans;
lli v;
};
void solve_same() {
if (accumulate(a, a + n, 0ll) % k != 0) {
cout << -1 << endl;
return;
}
vector <op> ans;
lli sum = a[0] / (k / __gcd(n, k));
for (int i = 0; ans.empty() || i; i = (i + k) % n) {
vector <int> tmp;
for (int j = i; j < i + k; ++j) tmp.pb(j % n);
ans.pb({tmp, sum});
}
cout << ans.size() << endl;
for (op i : ans) {
cout << i.v << ' ';
for (int j : i.ans) cout << j + 1 << ' ';
cout << endl;
}
}
void solve_count() {
if (accumulate(a, a + n, 0ll) % k != 0) {
cout << -1 << endl;
return;
}
priority_queue <pii> pq;
vector <op> ans;
for (int i = 0; i < n; ++i) pq.emplace(a[i], i);
while (pq.size() >= k) {
vector <int> id;
while (id.size() < k) {
id.pb(pq.top().Y); pq.pop();
}
for (int i : id) {
a[i]--;
if (a[i]) pq.emplace(a[i], i);
}
ans.pb({id, 1});
}
if (pq.empty()) {
cout << ans.size() << endl;
for (op i : ans) {
cout << i.v << ' ';
for (int j : i.ans) cout << j + 1 << ' ';
cout << endl;
}
} else {
cout << -1 << endl;
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
if (*max_element(a, a + n) == *min_element(a, a + n)) {
solve_same();
} else {
solve_count();
}
}
Compilation message (stderr)
nicegift.cpp: In function 'void solve_count()':
nicegift.cpp:56:22: warning: comparison of integer expressions of different signedness: 'std::priority_queue<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | while (pq.size() >= k) {
| ~~~~~~~~~~^~~~
nicegift.cpp:58:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
58 | while (id.size() < k) {
| ~~~~~~~~~~^~~
# | 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... |