/// adil sultanov | vonat1us
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
using namespace std;
using ll = long long;
using pii = pair<ll, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
const bool flag = 0;
const int N = 1e6+10;
pii t[N<<2];
ll a[N], n;
void build(int u = 0, int l = 0, int r = n) {
if (r-l == 1) {
t[u] = {a[l], l};
return;
}
int m = l+r >> 1;
build(u+u+1, l, m);
build(u+u+2, m, r);
t[u] = max(t[u+u+1], t[u+u+2]);
}
void modify(int pos, ll val, int u = 0, int l = 0, int r = n) {
if (r-l == 1) {
t[u].x = val;
return;
}
int m = l+r >> 1;
if (pos < m) {
modify(pos, val, u+u+1, l, m);
} else {
modify(pos, val, u+u+2, m, r);
}
t[u] = max(t[u+u+1], t[u+u+2]);
}
void ma1n() {
ll k;
cin >> n >> k;
ll sum = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i], sum += a[i];
}
if (sum%k) {
cout << -1, exit(0);
}
if (*min_element(a, a+n) == *max_element(a, a+n)) {
if (n%k == 0) {
cout << n/k << "\n";
for (int i = 0; i < n; i += k) {
cout << a[i] << " ";
for (int j = i; j < i+k; ++j) cout << j+1 << " ";
cout << "\n";
}
return;
}
if (a[0]%__gcd(n, k) != 0) {
cout << -1, exit(0);
}
if (__gcd(n, k) == 1) {
if (a[0]%k) {
cout << -1, exit(0);
}
cout << n << "\n";
for (int i = 0; i < n; ++i) {
cout << a[i]/k << " ";
for (int j = i; j < i+k; ++j) {
cout << (j%n)+1 << " ";
}
cout << "\n";
}
return;
}
vector<vector<int>> ans;
int st = 0;
do {
vector<int> p;
for (int i = st; i < st+k; ++i) {
p.pb(i%n);
}
ans.pb(p);
st = (st+k)%n;
} while (st != 0);
cout << sz(ans) << "\n";
for (auto v : ans) {
cout << a[0]/__gcd(n, k) << " ";
for (int x : v) cout << x+1 << " ";
cout << "\n";
}
return;
}
vector<int> ans;
build();
while (1) {
if (t[0].x == 0) break;
vector<pii> v;
for (int i = 0; i < k; ++i) {
if (t[0].x == 0) {
cout << -1, exit(0);
}
v.pb({t[0].y, t[0].x});
modify(t[0].y, 0);
}
ll d = v.back().y-(t[0].x ? t[0].x : 1)+1;
ans.pb(d);
for (pii& x : v) {
ans.pb(x.x+1);
modify(x.x, x.y-d);
}
}
cout << sz(ans)/(k+1) << "\n";
for (int i = 0; i < sz(ans); i += k+1) {
for (int j = i; j <= i+k; ++j) {
cout << ans[j] << " ";
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int ts = 1;
if (flag) {
cin >> ts;
}
while (ts--) {
ma1n();
}
return 0;
}
Compilation message
nicegift.cpp: In function 'void build(int, int, int)':
nicegift.cpp:41:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int m = l+r >> 1;
| ~^~
nicegift.cpp: In function 'void modify(int, ll, int, int, int)':
nicegift.cpp:52:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = l+r >> 1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
1 ms |
204 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
1 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
252 KB |
n=5 |
8 |
Correct |
1 ms |
332 KB |
n=8 |
9 |
Correct |
4 ms |
588 KB |
n=14 |
10 |
Correct |
2 ms |
460 KB |
n=11 |
11 |
Correct |
26 ms |
3652 KB |
n=50000 |
12 |
Correct |
32 ms |
3984 KB |
n=50000 |
13 |
Correct |
10 ms |
976 KB |
n=10 |
14 |
Correct |
26 ms |
1348 KB |
n=685 |
15 |
Correct |
30 ms |
1460 KB |
n=623 |
16 |
Correct |
15 ms |
1044 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
1 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
252 KB |
n=5 |
8 |
Correct |
1 ms |
332 KB |
n=8 |
9 |
Correct |
4 ms |
588 KB |
n=14 |
10 |
Correct |
2 ms |
460 KB |
n=11 |
11 |
Correct |
26 ms |
3652 KB |
n=50000 |
12 |
Correct |
32 ms |
3984 KB |
n=50000 |
13 |
Correct |
10 ms |
976 KB |
n=10 |
14 |
Correct |
26 ms |
1348 KB |
n=685 |
15 |
Correct |
30 ms |
1460 KB |
n=623 |
16 |
Correct |
15 ms |
1044 KB |
n=973 |
17 |
Correct |
29 ms |
1448 KB |
n=989 |
18 |
Correct |
15 ms |
956 KB |
n=563 |
19 |
Correct |
23 ms |
1192 KB |
n=592 |
20 |
Correct |
23 ms |
1228 KB |
n=938 |
21 |
Correct |
20 ms |
1024 KB |
n=747 |
22 |
Correct |
26 ms |
1152 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
22528 KB |
n=1000000 |
2 |
Correct |
163 ms |
13380 KB |
n=666666 |
3 |
Correct |
121 ms |
7236 KB |
n=400000 |
4 |
Correct |
191 ms |
19812 KB |
n=285714 |
5 |
Correct |
5 ms |
616 KB |
n=20000 |
6 |
Correct |
172 ms |
17220 KB |
n=181818 |
7 |
Correct |
3 ms |
452 KB |
n=10000 |
8 |
Incorrect |
30 ms |
3348 KB |
Taken too much stones from the heap |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
1 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
252 KB |
n=5 |
8 |
Correct |
1 ms |
332 KB |
n=8 |
9 |
Correct |
4 ms |
588 KB |
n=14 |
10 |
Correct |
2 ms |
460 KB |
n=11 |
11 |
Correct |
26 ms |
3652 KB |
n=50000 |
12 |
Correct |
32 ms |
3984 KB |
n=50000 |
13 |
Correct |
10 ms |
976 KB |
n=10 |
14 |
Correct |
26 ms |
1348 KB |
n=685 |
15 |
Correct |
30 ms |
1460 KB |
n=623 |
16 |
Correct |
15 ms |
1044 KB |
n=973 |
17 |
Correct |
29 ms |
1448 KB |
n=989 |
18 |
Correct |
15 ms |
956 KB |
n=563 |
19 |
Correct |
23 ms |
1192 KB |
n=592 |
20 |
Correct |
23 ms |
1228 KB |
n=938 |
21 |
Correct |
20 ms |
1024 KB |
n=747 |
22 |
Correct |
26 ms |
1152 KB |
n=991 |
23 |
Correct |
256 ms |
22528 KB |
n=1000000 |
24 |
Correct |
163 ms |
13380 KB |
n=666666 |
25 |
Correct |
121 ms |
7236 KB |
n=400000 |
26 |
Correct |
191 ms |
19812 KB |
n=285714 |
27 |
Correct |
5 ms |
616 KB |
n=20000 |
28 |
Correct |
172 ms |
17220 KB |
n=181818 |
29 |
Correct |
3 ms |
452 KB |
n=10000 |
30 |
Incorrect |
30 ms |
3348 KB |
Taken too much stones from the heap |
31 |
Halted |
0 ms |
0 KB |
- |