1700*D. Constant Palindrome Sum(差分&贪心)

Problem - 1343D - Codeforces

1700*D. Constant Palindrome Sum(差分&贪心)_第1张图片

1700*D. Constant Palindrome Sum(差分&贪心)_第2张图片

#include
using namespace std;
#define int long long
const int N=4e5+5;
int t,n,k,a[N],cnt[N],c[N];
signed main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		for(int i=1;i<=2*k;i++) c[i]=cnt[i]=0;
		for(int i=1;i<=n/2;i++){
			int x=a[i],y=a[n-i+1];
			c[min(x,y)+1]++;
			c[max(x,y)+k+1]--;
			cnt[x+y]++;
		}
		for(int i=3;i<=2*k;i++){
			c[i]+=c[i-1];
		}
		int res=0x3f3f3f3f;
		for(int i=2;i<=2*k;i++){
			res=min(res,c[i]+2*(n/2-c[i])-cnt[i]);
		}
		printf("%lld\n\n",res);
	}
	return 0;
}

你可能感兴趣的:(codeforces,算法,c++,数据结构,c语言,开发语言)