Submission #895372

# Submission time Handle Problem Language Result Execution time Memory
895372 2023-12-29T20:02:39 Z MilosMilutinovic JOI15_aaqqz (JOI15_aaqqz) C++14
100 / 100
102 ms 36072 KB
#include <bits/stdc++.h>

using namespace std;

int BruteForce(vector<int> a) {
  int n = (int) a.size();
  int res = 0;
  int L, R;
  for (int l = 0; l < n; l++) {
    for (int r = l; r < n; r++) {
      vector<int> seq;
      for (int i = l; i <= r; i++) {
        seq.push_back(a[i]);
      }
      sort(seq.begin(), seq.end());
      vector<int> new_a = a;
      for (int j = l; j <= r; j++) {
        new_a[j] = seq[j - l];
      }
      vector<vector<bool>> f(n, vector<bool>(n));
      for (int i = n - 1; i >= 0; i--) {
        for (int j = i; j < n; j++) {
          if (i == j) {
            f[i][j] = true;
            continue;
          }
          if (new_a[i] == new_a[j]) {
            if (i + 1 == j) {
              f[i][j] = true;
            } else {
              f[i][j] = f[i + 1][j - 1];
            }
          }
        }
      }
      for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
          if (f[i][j]) {
            res = max(res, j - i + 1);
            if (res == j - i + 1) {
              L = l;
              R = r;
            }
          }
        }
      }
    }
  }
  cout << "-- " << L << " " << R << '\n';
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, c;
  cin >> n >> c;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  while (true) {
    int ans = 0;
    {
      vector<int> cnt(c);
      for (int i = 0; i < n; i++) {
        cnt[a[i]] += 1;
      }
      ans = max(ans, *max_element(cnt.begin(), cnt.end()));
    }
    for (int rot = 0; rot < 2; rot++) {
      vector<vector<int>> dp(n, vector<int>(n));
      for (int i = 0; i < n; i++) {
        for (int j = n - 1; j >= i; j--) {
          if (i == j) {
            dp[i][j] = 1 + (i > 0 && j + 1 < n ? dp[i - 1][j + 1] : 0);
          } else {
            if (a[i] != a[j]) {
              dp[i][j] = 0;
              continue;
            }
            dp[i][j] = 1 + (i > 0 && j + 1 < n ? dp[i - 1][j + 1] : 0);
          }
        }
      }
      auto Extend = [&](int L, int R, bool f) {
        ans = max(ans, R - L + 1);
        if (!f) {
          int ptr = R;
          vector<int> cnt(c);
          for (int i = L - 1; i >= 0; i--) {
            if (i < L - 1 && a[i] < a[i + 1]) {
              break;
            }
            bool ok = true;
            while (ptr + 1 < n && cnt[a[i]] == 0) {
              ptr += 1;
              if (a[ptr] < a[i]) {
                ok = false;
              }
              cnt[a[ptr]] += 1;
            }
            if (!cnt[a[i]] || !ok) {
              break;
            }
            cnt[a[i]] -= 1;
            for (int j = (i == L - 1 ? 0 : a[i + 1]); j < a[i]; j++) {
              if (cnt[j] > 0) {
                ok = false;
                break;
              }
            }
            if (!ok) {
              break;
            }
            int foo = 0;
            if (L - i == ptr - R && i > 0 && ptr + 1 < n) {
              foo = dp[i - 1][ptr + 1] * 2;
            }
            ans = max(ans, R - L + 1 + (L - i) * 2 + foo);
          }
        } else {
          int mn = c;
          for (int i = L + 1; i < n; i++) {
            if (a[i] < a[L]) {
              mn = a[i];
              break;
            }
          }
          if (mn == c) {
            return;
          }
          int ptr = R;
          vector<int> cnt(c);
          int cnt_mn = 0;
          for (int i = L; i >= 0; i--) {
            if (i < L && a[i] < a[i + 1]) {
              break;
            }
            bool ok = true;
            while (ptr + 1 < n && cnt[a[i]] == 0) {
              ptr += 1;
              if (a[ptr] < mn) {
                break;
              }
              if (a[ptr] != mn) {
                if (a[ptr] < a[i]) {
                  ok = false;
                  break;
                }
                cnt[a[ptr]] += 1;
              } else {
                cnt_mn += 1;
              }
            }
            if (!cnt[a[i]] || !ok) {
              break;
            }
            cnt[a[i]] -= 1;
            for (int j = (i == L ? 0 : a[i + 1]); j < a[i]; j++) {
              if (cnt[j] > 0) {
                ok = false;
                break;
              }
            }
            if (!ok) {
              break;
            }
            while (true) {
              int foo = 0;
              if (L - i + 1 == ptr - R - cnt_mn && i > 0 && ptr + 1 < n) {
                foo = dp[i - 1][ptr + 1] * 2;
              }
              ans = max(ans, (L - i + 1) * 2 + cnt_mn + foo);
              if (ptr + 1 < n && a[ptr + 1] == mn) {
                cnt_mn += 1;
                ptr += 1;
              } else {
                break;
              }
            }
          }

          vector<int> cntL(c), cntR(c);
          ptr = R;
          cnt_mn = 0;
          for (int i = L; i >= 0; i--) {
            if (i < L && a[i + 1] > a[i]) {
              break;
            }
            cntL[a[i]] += 1;
            bool ok = true;
            while (ptr + 1 < n) {
              if (a[ptr + 1] < mn) {
                break;
              } else if (a[ptr + 1] == mn) {
                cnt_mn += 1;
              } else {
                if (a[ptr + 1] < a[i]) {
                  break;
                }
                cntR[a[ptr + 1]] += 1;
              }
              ptr += 1;
            }
            if (cntL[a[i]] > cntR[a[i]] || !ok) {
              break;
            }
            if (!ok) {
              break;
            }
            for (int j = (i == L ? 0 : a[i + 1]); j < a[i]; j++) {
              while (ptr > R && cntR[j] > cntL[j]) {
                if (a[ptr] == mn) {
                  cnt_mn -= 1;
                  ptr -= 1;
                  continue;
                }
                cntR[a[ptr]] -= 1;
                if (cntL[a[ptr]] > cntR[a[ptr]]) {
                  ok = false;
                  break;
                }
                ptr -= 1;
              }
              if (cntR[j] > cntL[j]) {
                ok = false;
                break;
              }
            }
            if (!ok) {
              break;
            }
            ans = max(ans, (L - i + 1) * 2 + cnt_mn);
          }
        }
      };
      for (int i = 0; i < n; i++) {
        Extend(i - dp[i][i] + 1, i + dp[i][i] - 1, false);
        Extend(i, i, false);
        if (i + 1 < n && a[i] == a[i + 1]) {
          Extend(i - dp[i][i + 1] + 1, i + dp[i][i + 1], false);
        }
      }
      for (int i = 0; i < n; i++) {
        Extend(i, i, true);
      }
      for (int i = 0; i < n; i++) {
        a[i] = c - a[i] - 1;
      }
      reverse(a.begin(), a.end());
    }
    cout << ans << '\n';
   break;
  }
  return 0;
}

/*
10 6
5 5 4 0 1 1 0 5 3 4

7 4
2 3 2 2 3 3 1
*/

Compilation message

aaqqz.cpp: In function 'int BruteForce(std::vector<int>)':
aaqqz.cpp:49:37: warning: 'R' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   cout << "-- " << L << " " << R << '\n';
      |                                     ^~~~
aaqqz.cpp:49:25: warning: 'L' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   cout << "-- " << L << " " << R << '\n';
      |                         ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 61 ms 35812 KB Output is correct
39 Correct 45 ms 35816 KB Output is correct
40 Correct 45 ms 35824 KB Output is correct
41 Correct 59 ms 35816 KB Output is correct
42 Correct 59 ms 35816 KB Output is correct
43 Correct 71 ms 36068 KB Output is correct
44 Correct 66 ms 35816 KB Output is correct
45 Correct 54 ms 35816 KB Output is correct
46 Correct 54 ms 35744 KB Output is correct
47 Correct 65 ms 35900 KB Output is correct
48 Correct 65 ms 35764 KB Output is correct
49 Correct 62 ms 36072 KB Output is correct
50 Correct 55 ms 35824 KB Output is correct
51 Correct 66 ms 35816 KB Output is correct
52 Correct 70 ms 35812 KB Output is correct
53 Correct 57 ms 35816 KB Output is correct
54 Correct 58 ms 35816 KB Output is correct
55 Correct 102 ms 35812 KB Output is correct
56 Correct 54 ms 35832 KB Output is correct
57 Correct 54 ms 35816 KB Output is correct
58 Correct 67 ms 35816 KB Output is correct
59 Correct 66 ms 35832 KB Output is correct
60 Correct 54 ms 35828 KB Output is correct
61 Correct 54 ms 35828 KB Output is correct
62 Correct 60 ms 35816 KB Output is correct
63 Correct 60 ms 35880 KB Output is correct
64 Correct 64 ms 35816 KB Output is correct
65 Correct 66 ms 35928 KB Output is correct
66 Correct 78 ms 35808 KB Output is correct
67 Correct 67 ms 35876 KB Output is correct