QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#884458 | #9729. Dividing Sequence | guosoun | TL | 1ms | 3584kb | C++17 | 2.4kb | 2025-02-06 08:32:24 | 2025-02-06 08:32:24 |
Judging History
answer
#include <bits/stdc++.h>
// #include "../cpp-dump/cpp-dump.hpp"
template <class T>
using V = std::vector<T>;
template <class T>
void chkmin(T &x, const T &y) {
if (x > y) x = y;
}
using ll = long long;
struct hope {
V<V<int>> dp;
V<int> a, b;
int r;
hope(V<int> a) : a(a) {
int n = a.size(), m = b.size();
dp.assign(n, V<int>(m + 1, 1e9));
r = 1e9;
if (!b.size()) r = a[0];
for (int i = 0; i < n; i++) {
auto tmp = !i ? V<int>(m + 1, 1e9) : dp[i - 1];
if (!i) tmp[0] = 0;
for (int j = 0; j <= m && j <= i; j++) {
if (j < m && b[j] == a[i]) chkmin(dp[i][j + 1], tmp[j]);
if (tmp[j] == -1) {
dp[i][j] = -1;
continue;
}
if (tmp[j] >= 1e9) continue;
int k = i - j;
if (k > m) chkmin(dp[i][j], tmp[j]);
if (k == m) chkmin(dp[i][j], a[i]);
if (k < m) {
if (a[i] < b[k]) chkmin(dp[i][j], -1);
if (b[k] == a[i]) chkmin(dp[i][j], tmp[j]);
}
}
if (i + 1 < n && dp[i][m] < a[i + 1]) chkmin(r, a[i + 1]);
if (i == n - 1 && dp[i][m] <= 0) r = 0;
}
if (r < 0) r = 0;
}
void push_back(int x) {
b.push_back(x);
int n = a.size(), m = b.size();
for (int i = 0; i < n; i++) dp[i].push_back(1e9);
r = 1e9;
if (!b.size()) r = a[0];
for (int i = 0; i < n; i++) {
for (int j = m - 1; j <= m && j <= i; j++) {
int tj = !i ? !j ? 0 : 1e9 : dp[i - 1][j];
if (j < m && b[j] == a[i]) chkmin(dp[i][j + 1], tj);
if (tj == -1) {
dp[i][j] = -1;
continue;
}
if (tj >= 1e9) continue;
int k = i - j;
if (k > m) chkmin(dp[i][j], tj);
if (k == m) chkmin(dp[i][j], a[i]);
if (k < m) {
if (a[i] < b[k]) chkmin(dp[i][j], -1);
if (b[k] == a[i]) chkmin(dp[i][j], tj);
}
}
if (i + 1 < n && dp[i][m] < a[i + 1]) chkmin(r, a[i + 1]);
if (i == n - 1 && dp[i][m] <= 0) r = 0;
}
if (r < 0) r = 0;
}
};
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
V<int> a(n);
for (int &x : a) std::cin >> x;
hope h(a);
while (int t = h.r) h.push_back(t);
std::cout << h.b.size() << '\n';
for (int i : h.b) std::cout << i << ' ';
std::cout << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3584kb
input:
5 5 3 1 2 3 2 3 1 1 2 3 3 3 3 5 1 3 1 3 1 5 2 2 1 3 3
output:
1 3 3 1 1 2 2 3 3 3 1 3 1 4 2 1 3 3
result:
ok 10 lines
Test #2:
score: -100
Time Limit Exceeded
input:
2000 4 2 4 4 1 1 1 3 2 3 2 4 4 4 3 1 1 1 2 3 2 4 1 3 2 1 3 4 1 1 2 4 2 4 2 3 1 2 3 3 4 3 4 1 2 2 1 2 1 1 4 1 4 2 4 2 1 3 4 4 2 4 2 5 2 4 1 4 3 4 3 4 4 3 3 1 4 4 2 1 1 5 2 1 3 2 3 4 3 2 3 4 2 1 2 1 2 4 4 1 3 2 2 1 2 4 4 2 4 4 1 3 2 2 4 1 1 1 1 1 2 4 4 4 4 3 1 1 1 1 1 4 4 1 3 2 1 2 2 1 5 3 1 1 1 1 5 4...