input
7
1 3 2 2 4 5 4
output
3
3 1 4 2 4 2 5
题意:给n个数排列之后使两边的数大于中间的数的情况最多并且要最优序列
思路:我们可以先找出有几个可以插入的数,然后按顺序插入就可以了
AC code
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
queue<int>p;
int n,a[120000],b[120000];
int main()
{
cin>>n;
memset(b,0,sizeof(b));//标记
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
if(n<3)
{
printf("0\n");
if(n==1)printf("%d",a[1]);
else printf("%d %d",a[1],a[2]);}
else
{
while(!p.empty())
p.pop();//清空队列
int q;
sort(a+1,a+n+1);
if((n-1)%2==0)q=(n-1)/2;
else q=n/2-1;//找出有几个数可以插入
int l=1,r=q+1,s=0,flag=0;
while(l<=q&&r<n)//从小到大插入
{
if(a[l]<a[r]&&a[l]<a[r+1])//保证两边的数大于中间的数
{
s++;
p.push(a[r]);
b[r]=1;
r++;
p.push(a[l]);
b[l]=1;
l++;
if(r==n)//如果r为最后的边界必须入队
{
p.push(a[r]);
b[r]=1;
}
}
else r++;
}
for(int i=1;i<=n;i++)
if(!b[i])p.push(a[i]);//从小到大没入队的入队
printf("%d\n",s);
while(!p.empty())
{
printf("%d",p.front());
if(p.size()!=1)printf(" ");
p.pop();
}
}
}