5.23 Vj B - Balanced Array

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//按照题意可知二分之n为偶数的时候存在这样的数组,按照题意分两部分存入数组即可
int main()
{
    ll t,n;
    cin>>t;
    for(int i=0; i<t; i++)
    {
        cin>>n;
        if(n/2%2==0)
        {
            ll a[n+1];
            ll s=0,k=1;
            for(int j=1; j<=n/2; j++)
            {
                a[j]=j*2;
                s+=a[j];
            }
            for(int j=n/2+1;j<n;j++)
            {
                a[j]=k;
                s-=k;
                k+=2;
            }
            a[n]=s;
            cout<<"YES"<<endl;
            for(int j=1;j<=n;j++)
            {
                cout<<a[j]<<" ";
            }
            cout<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}

上一篇:5.23 Vj A - Candies


下一篇:5.29 VJ F - Eugene and an array