/*
Condition:
- Sum of arrays must be divisible by k
- Maximum number must be smaller than or equal to (sum / k)
Solution:
- Divide array into k blocks
- ....
*/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 1001000
#define pp pair<long long, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
using namespace std;
int n, k;
vector< vector<pp> > block;
vector< vector<long long> > res;
long long a[N];
int main() {
#ifdef NERO
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
double stime = clock();
#else
//freopen(taskname".inp","r",stdin);
//freopen(taskname".out","w",stdout);
#endif //NERO
IO;
cin >> n >> k;
long long sum = 0, mx = 0;
FOR(i, 1, n) {
cin >> a[i];
mx = max(mx, a[i]);
sum += a[i];
}
long long average = (sum / k);
if (sum % k || mx > average) {
cout << -1;
return 0;
}
vector<pp> now;
long long temp = 0;
FOR(i, 1, n) {
temp += a[i];
if (temp >= average) {
now.push_back(pp(a[i] - (temp - average), i));
block.push_back(now);
temp -= average;
now.clear();
if (temp) now.push_back(pp(temp, i));
} else now.push_back(pp(a[i], i));
}
while (sum > 0) {
vector<long long> ans;
long long subtract = 1e18;
REP(i, k) subtract = min(subtract, block[i].back().first);
sum -= subtract * k;
ans.push_back(subtract);
REP(i, k) {
block[i].back().first -= subtract;
ans.push_back(block[i].back().second);
if (block[i].back().first == 0) block[i].pop_back();
}
res.push_back(ans);
}
cout << (int) res.size() << '\n';
REP(i, res.size()) {
for (long long x : res[i]) cout << x << ' '; cout << '\n';
}
#ifdef NERO
double etime = clock();
cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
#endif // NERO
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:12:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define REP(i, a) for (int i = 0; i < (a); ++i)
^
nicegift.cpp:76:5: note: in expansion of macro 'REP'
REP(i, res.size()) {
^~~
nicegift.cpp:77:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (long long x : res[i]) cout << x << ' '; cout << '\n';
^~~
nicegift.cpp:77:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (long long x : res[i]) cout << x << ' '; cout << '\n';
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
488 KB |
n=3 |
3 |
Correct |
2 ms |
608 KB |
n=3 |
4 |
Correct |
2 ms |
608 KB |
n=4 |
5 |
Correct |
2 ms |
608 KB |
n=4 |
6 |
Correct |
2 ms |
644 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
488 KB |
n=3 |
3 |
Correct |
2 ms |
608 KB |
n=3 |
4 |
Correct |
2 ms |
608 KB |
n=4 |
5 |
Correct |
2 ms |
608 KB |
n=4 |
6 |
Correct |
2 ms |
644 KB |
n=2 |
7 |
Correct |
2 ms |
644 KB |
n=5 |
8 |
Correct |
2 ms |
644 KB |
n=8 |
9 |
Correct |
2 ms |
644 KB |
n=14 |
10 |
Correct |
2 ms |
644 KB |
n=11 |
11 |
Correct |
31 ms |
5244 KB |
n=50000 |
12 |
Correct |
29 ms |
5244 KB |
n=50000 |
13 |
Correct |
2 ms |
5244 KB |
n=10 |
14 |
Correct |
2 ms |
5244 KB |
n=685 |
15 |
Correct |
2 ms |
5244 KB |
n=623 |
16 |
Correct |
2 ms |
5244 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
488 KB |
n=3 |
3 |
Correct |
2 ms |
608 KB |
n=3 |
4 |
Correct |
2 ms |
608 KB |
n=4 |
5 |
Correct |
2 ms |
608 KB |
n=4 |
6 |
Correct |
2 ms |
644 KB |
n=2 |
7 |
Correct |
2 ms |
644 KB |
n=5 |
8 |
Correct |
2 ms |
644 KB |
n=8 |
9 |
Correct |
2 ms |
644 KB |
n=14 |
10 |
Correct |
2 ms |
644 KB |
n=11 |
11 |
Correct |
31 ms |
5244 KB |
n=50000 |
12 |
Correct |
29 ms |
5244 KB |
n=50000 |
13 |
Correct |
2 ms |
5244 KB |
n=10 |
14 |
Correct |
2 ms |
5244 KB |
n=685 |
15 |
Correct |
2 ms |
5244 KB |
n=623 |
16 |
Correct |
2 ms |
5244 KB |
n=973 |
17 |
Correct |
3 ms |
5244 KB |
n=989 |
18 |
Correct |
3 ms |
5244 KB |
n=563 |
19 |
Correct |
3 ms |
5244 KB |
n=592 |
20 |
Correct |
4 ms |
5244 KB |
n=938 |
21 |
Correct |
3 ms |
5244 KB |
n=747 |
22 |
Correct |
4 ms |
5244 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
600 ms |
73636 KB |
n=1000000 |
2 |
Correct |
385 ms |
73636 KB |
n=666666 |
3 |
Correct |
179 ms |
73636 KB |
n=400000 |
4 |
Correct |
429 ms |
73636 KB |
n=285714 |
5 |
Correct |
12 ms |
73636 KB |
n=20000 |
6 |
Correct |
373 ms |
73636 KB |
n=181818 |
7 |
Correct |
9 ms |
73636 KB |
n=10000 |
8 |
Correct |
48 ms |
73636 KB |
n=6666 |
9 |
Correct |
4 ms |
73636 KB |
n=4000 |
10 |
Correct |
238 ms |
73636 KB |
n=2857 |
11 |
Correct |
5 ms |
73636 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
488 KB |
n=3 |
3 |
Correct |
2 ms |
608 KB |
n=3 |
4 |
Correct |
2 ms |
608 KB |
n=4 |
5 |
Correct |
2 ms |
608 KB |
n=4 |
6 |
Correct |
2 ms |
644 KB |
n=2 |
7 |
Correct |
2 ms |
644 KB |
n=5 |
8 |
Correct |
2 ms |
644 KB |
n=8 |
9 |
Correct |
2 ms |
644 KB |
n=14 |
10 |
Correct |
2 ms |
644 KB |
n=11 |
11 |
Correct |
31 ms |
5244 KB |
n=50000 |
12 |
Correct |
29 ms |
5244 KB |
n=50000 |
13 |
Correct |
2 ms |
5244 KB |
n=10 |
14 |
Correct |
2 ms |
5244 KB |
n=685 |
15 |
Correct |
2 ms |
5244 KB |
n=623 |
16 |
Correct |
2 ms |
5244 KB |
n=973 |
17 |
Correct |
3 ms |
5244 KB |
n=989 |
18 |
Correct |
3 ms |
5244 KB |
n=563 |
19 |
Correct |
3 ms |
5244 KB |
n=592 |
20 |
Correct |
4 ms |
5244 KB |
n=938 |
21 |
Correct |
3 ms |
5244 KB |
n=747 |
22 |
Correct |
4 ms |
5244 KB |
n=991 |
23 |
Correct |
600 ms |
73636 KB |
n=1000000 |
24 |
Correct |
385 ms |
73636 KB |
n=666666 |
25 |
Correct |
179 ms |
73636 KB |
n=400000 |
26 |
Correct |
429 ms |
73636 KB |
n=285714 |
27 |
Correct |
12 ms |
73636 KB |
n=20000 |
28 |
Correct |
373 ms |
73636 KB |
n=181818 |
29 |
Correct |
9 ms |
73636 KB |
n=10000 |
30 |
Correct |
48 ms |
73636 KB |
n=6666 |
31 |
Correct |
4 ms |
73636 KB |
n=4000 |
32 |
Correct |
238 ms |
73636 KB |
n=2857 |
33 |
Correct |
5 ms |
73636 KB |
n=2000 |
34 |
Correct |
19 ms |
73636 KB |
n=23514 |
35 |
Correct |
18 ms |
73636 KB |
n=23514 |
36 |
Correct |
2 ms |
73636 KB |
n=940 |
37 |
Correct |
2 ms |
73636 KB |
n=2 |
38 |
Correct |
113 ms |
73636 KB |
n=100000 |
39 |
Correct |
136 ms |
73636 KB |
n=100000 |
40 |
Correct |
3 ms |
73636 KB |
n=10 |
41 |
Correct |
3 ms |
73636 KB |
n=100 |
42 |
Correct |
7 ms |
73636 KB |
n=1000 |
43 |
Correct |
724 ms |
141064 KB |
n=1000000 |
44 |
Correct |
851 ms |
155112 KB |
n=1000000 |
45 |
Correct |
662 ms |
155112 KB |
n=666666 |
46 |
Correct |
501 ms |
155112 KB |
n=400000 |
47 |
Correct |
191 ms |
155112 KB |
n=2336 |
48 |
Correct |
456 ms |
155112 KB |
n=285714 |
49 |
Correct |
406 ms |
155112 KB |
n=181818 |
50 |
Correct |
329 ms |
155112 KB |
n=40000 |
51 |
Correct |
219 ms |
155112 KB |
n=20000 |
52 |
Correct |
222 ms |
155112 KB |
n=10000 |
53 |
Correct |
213 ms |
155112 KB |
n=6666 |
54 |
Correct |
213 ms |
155112 KB |
n=4000 |
55 |
Correct |
268 ms |
155112 KB |
n=2857 |
56 |
Correct |
206 ms |
155112 KB |
n=2000 |