2020-05-22 — 习题训练二-B

B - Balanced Array

题意:给定数n,求是否存在满足前n/2的数均为偶数,后n/2的数均为奇数,且前n/2个数之和与后n/2个数之和相等的数组.

解题思路:设偶数为2n,奇数为2n-1,可知偶数与奇数依次各相差1,若数组最后一个奇数为n+2n-1,则存在这样的数组.

ac代码:

 

#include<iostream>
using namespace std;
int main(){
  int t,n,i;
  cin>>t;
  while(t--){
    cin>>n;
    n=n/2;
    if((3*n-1)%2==0){
      cout<<"NO"<<endl;
    }
    else{
      cout<<"YES"<<endl;
      cout<<"2";
      for(i=4;i<=2*n;i+=2){
        cout<<" "<<i;
      }
      for(i=1;i<2*n-1;i+=2){
        cout<<" "<<i;
      }
      cout<<" "<<3*n-1<<endl;
    }
  }
  return 0;
}

 

posted @ 2020-05-23 10:29  nanmoon  阅读(95)  评论(0编辑  收藏  举报