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 ...
随机推荐
- express中路由设置的坑-----1
router.get('/commodities/sortable', utils.logged, function (req, res) { Commodity.find({force_top:tr ...
- Kali linux安装漏洞扫描工具Nessus指南
引子:Nessus是著名信息安全服务公司tenable推出的一款漏洞扫描与分析软件,号称是"世界上最流行的漏洞扫描程序,全世界超过75,000个组织在使用它".虽然这个扫描程序能够 ...
- android 回调
调函数(callback Function),顾名思义,用于回调的函数. 回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用 ...
- Ubuntu启动、停止、重新启动MySQL,查看MySQL错误日志、中文编码错误
1)启动: sudo /etc/init.d/mysql start 2)停止: sudo /etc/init.d/mysql stop 3)重新启动: sudo /etc/init.d/mysql ...
- 基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...
- oracle数据库获取指定表的列的相关信息
1.很多时候我们需要从数据库中获取指定表的所有列的相关属性,如 name,commens,datatype,datalength,pk等.下面就是制定的语句. select c.TABLE_NAME ...
- 从UIImage的矩阵变换看矩阵运算的原理
1.矩阵的基本知识: struct CGAffineTransform { CGFloat a, b, c, d; CGFloat tx, ty;}; CGAffineTransform CGAf ...
- ##DAY12 UITableViewCell自定义
##DAY12 UITableViewCell自定义 #pragma mark -------自定义视图步骤--------- 自定义视图步骤: 1)在自定义cell类中,将所有cell要显示的子视图 ...
- HDU4323-Magic Number(levenshtein distance-编辑距离)
描述: There are many magic numbers whose lengths are less than 10. Given some queries, each contains a ...
- jQuery实时获取checkbox状态问题
在最近的项目开发中,使用jQuery操作checkbox时,发现一个问题. Html代码如下: <body> <div> <inputtype="checkbo ...