[CF] C. Palindromic Subsequences - Codeforces Round 997 (Div. 2)
Solutions Codeforces 构造 回文 Easy-
Lastmod: 2025-01-18 周六 23:15:44

https://codeforces.com/contest/2056/problem/C

题目大意

给出数字 $n \ (6 \le n \le 100)$ 要求用 $[1, n]$ 的数字,构造一个 $n$ 长的,最长回文子序列数量大于 $n$ 的数组。

简要题解

其实很容易想到怎么能得到较多的最长回文子序列。首先两组相同字母之间不能包含,不然最长回文是这两组嵌套起来的,而不是单独算的,因此相交不包含的组是更好的 $x \dots y \dots x \dots y$。

于是考虑这样的排布 $123 \dots 123 \dots$。实际上这个排布只对 $6$ 不行,但 $6$ 以上都没问题,因为这个排布的排列数量是:

  1. 如果 $n$ 为偶数:$\frac{n}{2} (\frac{n}{2} - 1)$ 显然 $n > 6$ 时它 $> n$。
  2. $n$ 是奇数时(把多的放在两组中间):$\frac{n - 1}{2} \frac{n + 1}{2}$ 同样 $n >= 7$ 时其 $> n$。

(其实如果不给 $6$ 的下界,以及不给 $6$ 这个较为特殊的情况,这题也许会更难。)

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;
  if (n == 6) {
    cout << "1 1 2 3 1 2\n";
    return;
  }
  for (int i = 1; i <= (n + 1) / 2; i++) {
    cout << i << ' ';
  }
  for (int i = 1; i <= n / 2; i++) {
    cout << i << (" \n"[i == n / 2]);
  }
}

int main() {
  int t = 1; 
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] B. Find the Permutation - Codeforces Round 997 (Div. 2)
Next: [CF] E. Nested Segments - Codeforces Round 997 (Div. 2)