D. Constant Palindrome Sum
题目链接:https://codeforces.com/contest/1343/problem/D
想法:
假设 j = n - i + 1
我们知道有三种情况
1、 a[i] 和 a[j] 都不改变,
2、a[i] 或 a[j] 改变一个
3、a[i] 和 a[j] 都改变
我们考虑这些改变的范围对于此时a[i] + a[j] 的贡献,最后取枚举 a[i] + a[j] 的值找出里面贡献和最小的那个就可以了
#pragma GCC optimize(3, "Ofast", "inline")//O3优化 #pragma GCC optimize(2)//O2优化 #include <algorithm> #include <string> #include <cstring> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <cmath> #include <cstdio> #include <iomanip> #include <ctime> #include <bitset> #include <cmath> #include <sstream> #include <iostream> #define ll long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f3f3f3f3f #define max(a, b) (a>b?a:b) #define min(a, b) (a<b?a:b) const double eps = 1e-10; const int maxn = 4e5 + 10; const int MOD = 998244353; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; ll a[maxn],sum[maxn]; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n,k; cin >> n >> k; for (int i = 1;i <= n;i++) cin >> a[i]; for (int i = 1;i <= 2 * k;i++) sum[i] = 0; for (int i = 1;i <= n/2;i++) { int j = n - i + 1; sum[2] += 2; sum[min(a[i],a[j]) + 1] -= 2; sum[min(a[i],a[j]) + 1] += 1; sum[a[i] + a[j]] -= 1; sum[a[i] + a[j] + 1] += 1; sum[max(a[i],a[j]) + k + 1] -= 1; sum[max(a[i],a[j]) + k + 1] += 2; sum[2 * k + 1] -= 2; } ll ans = INF; for (int i = 2;i <= 2 * k;i++) { sum[i] = sum[i-1] + sum[i]; ans = min(ans,sum[i]); } cout << ans << endl; } return 0; }