CF1419 D2. Sage's Birthday (hard version)
思路(贪心):
排序后分奇偶,小的放在奇数位,大的放在偶数位,最后for一遍查找满足条件的解即可(蒟蒻啥都不会,只能借鉴他人思路)
链接:(10条消息) D2. Sage‘s Birthday (hard version)(构造+贪心)_C_Dreamy的博客-CSDN博客
代码:
#include#define ll long long using namespace std; typedef pair PII; const int N = 1e5 + 10, mod = 1e9 + 7; int n; int a[N], ans[N]; void solve() { cin >> n; for (int i = 1; i <= n; i ++) { cin >> a[i]; } sort(a + 1, a + 1 + n); int num = 0; int p = 1; for (int i = 2; i <= n; i += 2) { ans[i] = a[p]; p ++; } for (int i = 1; i <= n; i += 2) { ans[i] = a[p]; p ++; } for (int i = 2; i <= n; i += 2) { if(ans[i] < ans[i - 1] && ans[i] < ans[i + 1]) num ++; } cout << num << endl; for (int i = 1; i <= n; i ++) cout << ans[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(0); //int T; //cin >> T; //while(T --) //{ solve(); //} return 0; }