1 solutions

  • 2
    @ 2025-1-2 17:02:39

    详细看代码

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int n, t, cnt1, cnt2, a[1000005], b[1000005];
    //cnt1,cnt2用来计数,a和b数组用来存数
    
    signed main() {
    	cin >> n;
    	for (int i = 1; i <= n; i++) {
    		t += i;
    	}
    	if (t % 2 != 0) {
    		cout << "NO" << endl;
    		return 0;
    	}//判断YES或NO
    	cout << "YES" << endl;
    	int t1 = t / 2, t2 = t / 2, flag = 1;
    	//两组数t1和t2各有t的一半,flag表示当前是第一组还是第二组
    	for (int i = n; i >= 1; i--) {
    		if (flag == 1) {
    			if (t1 >= i) {
    				a[++cnt1] = i;
    				t1 -= i;
    			} else {
    				b[++cnt2] = i;
    				t2 -= i;
    			}
    			//如果当前组不可以存入现在的数字,就更换另外一组
    			flag = 2;
    		} else if (flag == 2) {//同理
    			if (t2 >= i) {
    				b[++cnt2] = i;
    				t2 -= i;
    			} else {
    				a[++cnt1] = i;
    				t1 -= i;
    			}
    			flag = 1;
    		}
    	}
    	//输出部分
    	cout << cnt1 << endl;
    	for (int i = cnt1; i >= 1; i--) {
    		cout << a[i] << " ";
    	}
    	cout << endl << cnt2 << endl;
    	for (int i = cnt2; i >= 1; i--) {
    		cout << b[i] << " ";
    	}
    
    
    	return 0;
    }
    /*
    推算过程,括号里的表示当前组的t1和t2值
    14
    7(7) 5(2) 2(0)
    6(8) 4(4) 3(1) 1(0)
    
    */
    

    Information

    ID
    4621
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    35
    Accepted
    18
    Uploaded By