codeforces 626E. Simple Skewness 三分
给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大。
先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, 平均值最大, 肯定是它左边的数是靠近他的那几个数, 右边的数是最右边的那几个数。 然后所有情况取最大值。
三分的写法lmid = (l*2+r)/2, rmid = (l+r*2+2)/3, 学到了。
并且求平均值最好不要除,比如说平均数-中位数, 那么写成 这几个数的和-中位数*长度。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
ll a[maxn], sum[maxn];
int n;
ll cal(int len, int pos) {
ll num = sum[pos]-sum[pos-len-]+sum[n]-sum[n-len];
return num;
}
int main()
{
cin>>n;
for(int i = ; i<=n; i++) {
scanf("%I64d", &a[i]);
}
if(n == || n == ) {
cout<<<<endl;
cout<<a[]<<endl;
return ;
}
sort(a+, a++n);
ll maxx = ;
int len = , mid = ;
for(int i = ; i<=n; i++)
sum[i] = sum[i-]+a[i];
for(int i = ; i<=n-; i++) {
int l = , r = min(i-, n-i);
while(l<r) {
int lmid = (*l+r)/;
int rmid = (l+*r+)/;
ll ans1 = cal(lmid, i);
ll ans2 = cal(rmid, i);
if(ans1*(*rmid+)<(*lmid+)*ans2) {
l = lmid+;
} else {
r = rmid-;
}
}
ll tmp = (sum[i]-sum[i-l-]+sum[n]-sum[n-l])-(*l+)*a[i];
if(tmp*(*len+)>maxx*(*l+)) {
maxx = tmp;
len = l;
mid = i;
}
}
cout<<*len+<<endl;
for(int i = mid-len; i<=mid; i++)
printf("%d ", a[i]);
for(int i = n-len+; i<=n; i++)
printf("%d ", a[i]);
return ;
}
codeforces 626E. Simple Skewness 三分的更多相关文章
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces 626E Simple Skewness 「数学」「二分」
题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...
- codeforces626E.Simple Skewness(三分)
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. ...
- 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分
E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...
- 【CodeForces 626E】Simple Skewness
题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...
- CodeForces - 344B Simple Molecules (模拟题)
CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...
- CodeForces 570B Simple Game 概率
原题: http://codeforces.com/contest/570/problem/B 题目: Simple Game time limit per test1 second memory l ...
- codeforces 962F.simple cycle(tarjan/点双连通分量)
题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
随机推荐
- c语言函数---M
书画小说软件 制作更满意的读.更舒心的写.更轻松的公布 最全古典小说网 由本软件公布所得 main()主函数 每一C 程序都必须有一main()函数, 能够依据自己的爱好把它放在程序的某 个地方.有些 ...
- LeetCode第四题,Add Two Numbers
题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
宕机监控报警程序 一. 需求来源 宕机对运维人员来说,最痛苦了.怎样检測一台server是否还在正常执行,假设该server宕机,怎样在第一时间监測到并通知一线运维人员进行维护,最大化降低损失. ...
- 在 Visio 中录制宏
在“开发工具”选项卡上,单击“录制宏”.(如果您看不到“开发工具”选项卡,请参阅下面的“显示‘开发工具’选项卡”.) 在“宏名”框中,键入宏名称. 在“快捷键”框中,键入与 Ctrl 键一起使用可运行 ...
- jquery + ajax调用后台方法
前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...
- 决策树ID3算法[分类算法]
ID3分类算法的编码实现 <?php /* *决策树ID3算法(分类算法的实现) */ /* *求信息增益Grain(S1,S2) */ //-------------------------- ...
- 如何将mongodb bson文件转成json文件
使用mongodb自带的命令 bsondump collection.bson > collection.json
- QT小记之在VS2005中使用(设置QMAKESPEC环境变量,以及编译QT Lib)
QT的结构很清晰明了,看过第一个HELLO WORLD便爱上了它,感觉CEGUI有借鉴过QT的设计.如何在Windows平台下使用QT开发?一,下载SDK包请去官网(QT被NOKIA收购,貌似使用协议 ...
- Visual Studio 中用管理员权限运行、调试程序
原文:Visual Studio 中用管理员权限运行.调试程序 一个Sample小程序,用于验证WoW64的Windows Registry的读写访问.在Visual Studio 2010中调试运行 ...
- Bzoj2034 2009国家集训队试题 最大收益 贪心+各种优化+二分图
这个题真的是太神了... 从一開始枚举到最后n方的转化,各种优化基本都用到了极致.... FQW的题解写了好多,个人感觉我全然没有在这里废话的必要了 直接看这里 各种方法真的是应有尽有 大概说下 首先 ...