Topic: Give you an array with an even number of length n, you can modify an element to a value no greater than k at a time, requiring that each a [i] + a [n-i + 1] be equal, and find the minimum number of operations Times
Solution: Suppose the sum of each pair is sum, the small one is mn, and the big one is mx;
Enumerate all numbers x of [2,2 * k]:
We consider each pair of corresponding numbers, there are three cases: change one number , change two numbers , do not change
1. Change a number : when x∈ [mn + 1, mx + k]; ==》 b [mn + 1] +, 1, b [mx + k + 1] -1;
2. The two numbers : when x∈ [2, mn] || [mx + k + 1,2 * k]; ==》 b [2] + 2, b [mn + 1] -2; b [ mx + k + 1] + 2, b [2 * k] (the last one, no need to change)
3. Don't change : x == sum
So we record a difference array b [i] for each logarithm, maintain the interval and then restore.
Code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <unordered_set> 12 #include <unordered_map> 13 #define ll long long 14 #define fi first 15 #define se second 16 #define pb push_back 17 #define me memset 18 const int N = 1e6 + 10; 19 const int mod = 1e9 + 7; 20 using namespace std; 21 typedef pair<int,int> PII; 22 typedef pair<long,long> PLL; 23 24 int t; 25 int n,k,a[N]; 26 int sum; 27 int mx,mn; 28 int main() { 29 ios::sync_with_stdio(false); 30 cin>>t; 31 while(t--){ 32 cin>>n>>k; 33 int b[2*k+10]; 34 me(b,0,sizeof(b)); 35 for(int i=1;i<=n;++i) cin>>a[i]; 36 37 for(int i=1;i<=n/2;++i){ 38 sum=a[i]+a[n-i+1]; 39 mn=min(a[i],a[n-i+1]); 40 mx=max(a[i],a[n-i+1]); 41 42 b[2]+=2; 43 b[mn+1]--; 44 b[mx+k+1]++; 45 b[sum]--; 46 b[sum+1]++; 47 } 48 int res=b[2]; 49 for(int i=3;i<=2*k;++i){ 50 b[i]+=b[i-1]; 51 res=min(res,b[i]); 52 } 53 printf("%d\n",res); 54 } 55 56 return 0; 57 }