CF626E. Simple Skewness
Solution
先排序。
n
n
n为奇数时,枚举中位数,两边贪心地选择最大的数,显然平均数是凸函数,三分即可确定最值。
当
n
n
n为偶数时,必然选择最大值,然后转化为
n
n
n为奇数的情况。
PS:CF只有一个 n n n是偶数的数据:
2
1 2
所以特判即可QvQ。
Code
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
#define int ll
using namespace std;
template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;
const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=1000005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
int f=1,x=0; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
return x*f;
}
PR id;
vector<int> Ans;
int s[MAXN],a[MAXN],n;
int check(int x,int y) { return (s[x]-s[x-y-1])+(s[n]-s[n-y]); }
signed main()
{
n=read(),id=MP(n,0);
for (int i=1;i<=n;i++) a[i]=read();
if (n==2) { printf("2\n%lld %lld\n",a[1],a[2]); return 0; }
sort(a+1,a+n+1);
for (int i=1;i<=n;i++) s[i]=s[i-1]+a[i];
for (int i=1;i<=n;i++)
{
int l=(a[i]==0),r=min(i-1,n-i);
while (r-l>5)
{
int mid1=l+(r-l)/3,mid2=r-(r-l)/3;
if ((check(i,mid1)-a[i]*(mid1*2+1))*(mid2*2+1)>(check(i,mid2)-a[i]*(mid2*2+1))*(mid1*2+1)) r=mid2;
else l=mid1;
}
for (int j=l;j<=r;j++)
if ((check(id.fi,id.se)-a[id.fi]*(id.se*2+1))*(j*2+1)<(check(i,j)-a[i]*(j*2+1))*(id.se*2+1)) id=MP(i,j);
}
printf("%lld\n",id.se*2+1);
for (int i=id.fi-id.se;i<=id.fi;i++) printf("%lld ",a[i]);
for (int i=n-id.se+1;i<=n;i++) printf("%lld ",a[i]);
return 0;
}