2 条题解

  • 0
    @ 2026-9-1 13:31:19
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        long long sum = 1LL * n * (n + 1) / 2;
        if(sum % 2 != 0)
        {
            cout << "NO" << endl;
            return 0;
        }
        cout << "YES" << endl;
        long long target = sum / 2;
        vector<int> A, B;
        for(int i = n; i >= 1; i--)
        {
            if(target >= i)
            {
                A.push_back(i);
                target -= i;
            }
            else
            {
                B.push_back(i);
            }
        }
        cout << A.size() << endl;
        for(auto v : A) cout << v << " ";
        cout << endl;
        cout << B.size() << endl;
        for(auto v : B) cout << v << " ";
        return 0;
    }
    
    
    • 0
      @ 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)
      
      */
      
      • 1

      信息

      ID
      4621
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      144
      已通过
      49
      上传者