B. Balanced Array

本文探讨了一个编程挑战,即如何构造一个长度为n的数组,使其前半部分元素之和等于后半部分。文章提供了问题的详细描述,输入输出格式,以及一个示例性的解决方案代码,使用C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

B. Balanced Arraytime

**
**

limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
**

You are given a positive integer nnnn22You want to construct the array aannThe first n2n2aa22the second n2n2aa22all elements of aathe sum of the first half equals to the sum of the second half (∑i=1n2ai=∑i=n2+1nai∑i=1n2ai=∑i=n2+1naiIf there are multiple answers, you can print any. It is not guaranteed that the answer exists.You have to answer ttInputThe first line of the input contains one integer tt1≤t≤1041≤t≤104ttThe only line of the test case contains one integer nn2≤n≤2⋅1052≤n≤2⋅105nn22It is guaranteed that the sum of nn2⋅1052⋅105∑n≤2⋅105∑n≤2⋅105OutputFor each test case, print the answer — “NO” (without quotes), if there is no suitable answer for the given test case or “YES” in the first line and any suitable array a1,a2,…,ana1,a2,…,an1≤ai≤1091≤ai≤109Example
input
2
4
6
8
10
output
NO
YES
2 4 1 5
NO
YES
2 4 6 8 1 3 5 11
NO

源代码:

// An highlighted block
var foo = 'bar';
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include <algorithm>
using namespace std;
int main()
{
 int t,n,i;
 cin>>t;
 while(t--)
 {
   cin>>n;
   int ll=n/2,a,p,sum=0,num=0;
   if(n==2) {cout<<"NO"<<endl;continue;} 
   if(ll%2==0){
    cout<<"YES"<<endl;
    a=2;p=1;
    for(i=0;i<ll;i++){
     cout<<a<<" ";
     sum+=a;
     a+=2;
    }
      for(i=ll;i<n;i++){
       cout<<p<<" ";
       num+=p;
       if(i==n-2)
       p=sum-num;
       else p+=2;
   }
   printf("\n");
   }
   else cout<<"NO"<<endl;
 }
 return 0; 
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值