QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236246 | #3030. Fantastic Compression | ahsoltan# | AC ✓ | 535ms | 22116kb | C++20 | 2.7kb | 2023-11-03 19:09:54 | 2023-11-03 19:09:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
#define ir(x,a,b) ((a) <= (x) && (x) <= (b))
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define foru(i, n) for (int i = 0; i < n; ++i)
#define fori(i,a,n) for (int i = a; i < n; ++i)
// #ifdef LOCAL
// auto& operator<<(auto&, pair<auto, auto>);
// template<typename T, typename = T::value_type>
// auto& operator<<(auto& o, T x) requires (!same_as<T, string>) {
// o << "{";
// string s;
// for (auto i : x) {
// o << s << i;
// s << ", ";
// }
// return o << "}";
// }
// auto& operator<<(auto& o, pair<auto, auto> p) {
// return o << "(" << p.first << ", " << p.second << ")";
// }
// #define debug(x..) cerr << "["#x"]:",[](auto...$){((cerr<<" "<<$),...)<<endl;}(x)
// #else
// #define debug(...) 2137
// #endif
const int N = 25025;
int n, k;
int s[N];
int mx[6];
void solve() {
cin >> n >> k;
for (int i = 0; i < n - k + 1; i++) {
cin >> s[i];
}
for (int i = 0; i < k; i++) {
mx[i] = i;
ll b = 0;
ll pref = 0;
for (int j = i + k; j < n; j += k) {
// roznica aj - a(j - k)
int d = s[j - k + 1] - s[j - k];
pref += d;
if (pref > b) {
b = pref;
mx[i] = j;
} else if (pref == b) {
cout << 0 << '\n';
return;
}
}
}
/*for (int i = 0; i < k; i++) {
cerr << i << ' ' << mx[i] << '\n';
}*/
vector<int> p(k);
iota(p.begin(), p.end(), 0);
vector<vector<int>> ans;
do {
vector<bool> used(n + 1);
vector<int> x(n);
bool ok = true;
for (int i = 0; i < k; i++) {
int a = n;
while (used[a]) {
a--;
}
int st = mx[p[i]];
x[st] = a;
used[a] = true;
for (int j = st - k; j >= 0; j -= k) {
ll b = x[j + k] + s[j] - s[j + 1];
if (b < 1 || b > n || used[b]) {
ok = false;
break;
}
used[b] = true;
x[j] = b;
}
for (int j = st + k; j < n; j += k) {
ll b = x[j - k] + s[j - k + 1] - s[j - k];
if (b < 1 || b > n || used[b]) {
ok = false;
break;
}
used[b] = true;
x[j] = b;
}
if (!ok) {
break;
}
}
ll xs = 0;
for (int i = 0; i < k; i++) {
xs += x[i];
}
ok &= xs == s[0];
if (ok) {
ans.push_back(x);
}
} while (next_permutation(p.begin(), p.end()));
sort(ans.begin(), ans.end());
cout << ssize(ans) << '\n';
for (auto& i : ans) {
for (int j : i) {
cout << j << ' ';
}
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 3496kb
input:
964 5 4 10 11 5 3 9 8 10 5 2 9 6 5 4 4 4 9 4 4 9 5 4 13 11 5 3 11 10 10 4 2 4 6 3 5 4 12 13 5 3 9 12 9 5 2 9 8 6 3 5 3 10 8 7 5 3 7 10 9 5 4 13 14 5 3 7 8 11 4 2 6 7 4 4 4 11 4 3 8 6 5 3 7 6 6 5 3 10 9 11 5 4 10 11 5 3 12 9 8 5 5 15 5 3 9 10 7 5 5 15 4 2 4 7 6 5 2 4 5 9 7 5 2 6 3 6 7 5 3 7 6 9 4 2 3...
output:
6 4 1 2 3 5 4 1 3 2 5 4 2 1 3 5 4 2 3 1 5 4 3 1 2 5 4 3 2 1 5 1 2 3 4 1 5 1 5 4 2 3 1 0 0 6 4 1 3 5 2 4 1 5 3 2 4 3 1 5 2 4 3 5 1 2 4 5 1 3 2 4 5 3 1 2 0 0 6 2 1 4 5 3 2 1 5 4 3 2 4 1 5 3 2 4 5 1 3 2 5 1 4 3 2 5 4 1 3 2 1 5 3 4 2 2 4 3 5 1 0 1 3 5 2 1 4 1 2 4 1 5 3 6 1 3 4 5 ...
result:
ok 21593 lines
Test #2:
score: 0
Accepted
time: 37ms
memory: 3764kb
input:
11 23853 5 56129 66231 66759 74651 77738 77157 85513 66445 73648 57593 74488 61493 64993 60263 75748 73267 81681 86675 92728 73438 57233 47483 52402 42838 42284 58984 70785 56245 55687 58425 43712 34748 37286 34598 31262 36760 48165 57037 66885 67122 67572 62188 51356 32408 33791 44793 45602 58279 6...
output:
1 1955 20238 7208 20162 6566 12057 20766 15100 23249 5985 20413 1698 22303 7194 22880 7418 5198 17573 22679 20399 15832 10192 23626 3389 4194 6082 15111 14062 2835 20894 17883 571 13504 5573 6181 8919 3109 10816 2237 11679 20324 11981 20664 2474 12129 14940 1149 1716 3857 23131 15749 13826 5377 8992...
result:
ok 16 lines
Test #3:
score: 0
Accepted
time: 535ms
memory: 22116kb
input:
7 6666 6 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 167...
output:
720 1 1112 2223 3334 4445 5556 2 1113 2224 3335 4446 5557 3 1114 2225 3336 4447 5558 4 1115 2226 3337 4448 5559 5 1116 2227 3338 4449 5560 6 1117 2228 3339 4450 5561 7 1118 2229 3340 4451 5562 8 1119 2230 3341 4452 5563 9 1120 2231 3342 4453 5564 10 1121 2232 3343 4454 5565 11 1122 2233 3344 4455 55...
result:
ok 971 lines