QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#884508#9729. Dividing SequenceguosounTL 0ms3712kbC++172.4kb2025-02-06 08:55:372025-02-06 08:55:38

Judging History

This is the latest submission verdict.

  • [2025-02-06 08:55:38]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3712kb
  • [2025-02-06 08:55:37]
  • Submitted

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].back() = 1e9, dp[i].push_back(1e9);
    r = 1e9;
    if (!b.size()) r = a[0];
    for (int i = 0; i < n; i++) {
      for (int j = std::max(m - 2, 0); 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 (a[i] == b[k]) 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: 0ms
memory: 3712kb

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: 0
Accepted
time: 0ms
memory: 3584kb

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...

output:

3
2 4 4 
1
1 
2
2 3 
2
4 3 
1
1 
1
3 
3
1 3 2 
1
4 
1
4 
2
2 3 
2
3 4 
3
1 2 2 
1
1 
4
1 4 2 4 
2
1 3 
1
4 
2
2 4 
3
3 4 4 
3
1 4 4 
1
1 
1
2 
1
3 
2
1 2 
1
2 
1
4 
2
1 2 
1
4 
1
3 
2
2 4 
1
1 
1
1 
1
2 
2
4 4 
1
1 
1
1 
1
4 
3
1 3 2 
1
2 
1
3 
1
4 
2
2 4 
2
1 4 
1
3 
1
3 
1
3 
1
3 
2
1 3 
1
3 
1
3 ...

result:

ok 4000 lines

Test #3:

score: -100
Time Limit Exceeded

input:

1000
9
2 4 5 1 3 3 5 3 1
9
1 3 5 1 1 5 3 5 1
10
3 3 3 1 1 5 2 2 5 2
10
4 2 2 2 1 1 4 4 1 3
9
5 1 3 4 5 5 5 1 3
9
3 5 1 2 5 4 5 4 2
9
1 3 4 4 4 5 4 2 1
7
1 3 4 4 4 3 4
7
2 1 3 3 4 2 1
6
3 2 1 3 3 1
7
3 2 2 4 4 3 2
8
2 1 5 3 4 1 4 1
6
3 2 4 5 1 1
9
3 3 3 4 3 4 2 2 4
10
5 3 2 3 5 5 3 1 5 4
10
4 3 2 1 2...

output:


result: