https://codeforces.com/contest/1419/problem/D2
做法:
可以先给数组排个顺序(从大到小),然后选择后(n/2+1)个元素插到前面的大数字之前,
一个样例:
6
2 2 2 2 2 1
输出
1
2 2 2 1 2 2
写了很久这个脑残题,很难过。。。。
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 2e5+11;
typedef long long ll;
ll list[maxn];
bool bml(int a,int b){
return a>b;
}
vector<int>ins;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>list[i];
}
sort(list + 1,list+1+n,bml);
int j = (n+2)/2 + 1;
for(int i=1;i<=n;i++,j++){
if(i <= (n+2)/2) ins.push_back(list[i]);
if(j <= n) ins.push_back(list[j]);
}
int ans = 0;
for(int i=1;i<ins.size()-1;i++){
if(ins[i] < ins[i-1] && ins[i] < ins[i+1]) ans++;
}
cout<<ans<<endl;
for(int i=0;i<ins.size();i++){
cout<<ins[i]<<" ";
}
cout<<endl;
return 0;
}