zoukankan      html  css  js  c++  java
  • [CF Contest] Balanced Array 题解

    这道题可以将前 (frac{n}{2}) 个数的和(即(2,4,6,8,10...n))存起来,最后令后(frac{n}{2}-1)个数(即(1,3,5,7,9...n-3))的和减去前 (frac{n}{2}) 个数的和,得到第 (n) 个数的值。而且另外地,如果(frac{n}{2} mod 2=1)直接输出 NO 。

    (Code:)

    #include <iostream>
    using namespace std;
    int main() {
    	int t;
    	cin>>t;
    	for(int i=1; i<=t; i++) {
    		int n;
    		cin>>n;
    		if((n/2)%2==1) {			//特判
    			cout<<"NO"<<endl;
    		} else {
    			int ans=0,aans=0;
    			cout<<"YES"<<endl;
    			for(int j=2; j<=n; j+=2) {			//偶数
    				cout<<j<<' ';
    				ans+=j;						//累加
    			}
    			for(int j=1; j<=n; j+=2) {
    				if(n<=j+1)
    					cout<<ans-aans<<endl;		//第n个数的值
    				else {
    					cout<<j<<' ';				//奇数
    					aans+=j;
    				}
    			}
    		}
    	}
    }
    
  • 相关阅读:
    SCUT
    模板
    重链剖分
    树的重心
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
  • 原文地址:https://www.cnblogs.com/Inversentropir-36/p/13202041.html
Copyright © 2011-2022 走看看