提交时间:2024-12-13 20:06:18

运行 ID: 35598

#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int t, n; int a[200010]; int c[200010]; bool isPal(int x) { int l = 1, r = n; while (l <= r) { if (a[l] != a[r]) { if (a[l] == x) l++; if (a[r] == x) r--; if (l <= r && a[l] != a[r]) return false; } l++, r--; } return true; } int main() { // freopen("flogic.in", "r", stdin); // freopen("flogic.out", "w", stdout); scanf("%d", &t); while (t--) { scanf("%d", &n); int cnt = 0; for (int i = 1; i <= n; i++) c[i] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); cnt += 1 - c[a[i]]; c[a[i]] = 1; } int l = 1, r = n; bool ansIsFound = false; while (l <= r) { if (a[l] != a[r]) { bool f1 = isPal(a[l]); bool f2 = isPal(a[r]); int ty = 0; if (f1 && f2) ty = 3; else if (f1) ty = 2; else if (f2) ty = 1; if (ty == 0) { ansIsFound = true; printf("0\n\n"); break; } else if (ty == 1) { ansIsFound = true; printf("1\n%d\n", a[r]); break; } else if (ty == 2) { ansIsFound = true; printf("1\n%d\n", a[l]); break; } else { ansIsFound = true; int x = min(a[l], a[r]); int y = max(a[l], a[r]); printf("2\n%d %d\n", x, y); break; } } l++, r--; } if (!ansIsFound) { cout << cnt << endl; for (int i = 1; i <= n; i++) if (c[i]) cout << i << " "; cout << endl; } } // fclose(stdin); // fclose(stdout); return 0; }