其實主要是在考貪婪演算法
但比較需要注意的是要用
unordered_set,會比較快
#include <bits/stdc++.h>
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
int i;
vector<int> ans, li;
unordered_set<int> st;
int main(){ io;
ll n, tot=0, i, half=0,cnt=0;
cin >> n;
for (i=1; i<=n ; i++) tot+=i;
if (tot%2!=0){
cout << "NO";
return 0; //exit()
}
cout <<"YES"<< "\n";
half = tot/2;
for (i=n;i>0;i--){
if (cnt+i <= half){
ans.push_back(i);
st.insert(i);
cnt+=i;
}
}
cout << ans.size()<< "\n";
for (auto i :ans) cout << i << " ";
for (i=n;i>0;i--){
if (!st.count(i)){
li.push_back(i);
}
}
cout << "\n" << li.size()<< "\n";
for (auto x : li) {
cout << x << ' ';
}
}
Leave a Reply