939E - Maximize!

思路:

贪心:最后的集合是最大值+前k小个

因为平均值时关于k的凹形函数,所以可以用三分求最小值

又因为后面的k肯定比前面的k大,所以又可以双指针

三分:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pli pair<ll,int>
#define mem(a,b) memset(a,b,sizeof(a)) const int N=5e5+;
ll sum[N],cnt=;
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cout<<fixed<<setprecision();
int q,t,x;
cin>>q;
while(q--){
cin>>t;
if(t==){
cin>>x;
sum[++cnt]=sum[cnt-]+x;
}
else{
int l=,r=cnt-,m1=(l+l+r)/,m2=(l+r+r)/;
while(l<r){
if((double)(sum[m1]+x)/(m1+)>=(double)(sum[m2]+x)/(m2+)){
if(l==m1)break;
else l=m1;
}
else{
if(r==m2)break;
else r=m2;
}
m1=(l+l+r)/,m2=(l+r+r)/;
}
double tt=(double)(sum[l]+x)/(l+);
for(int i=l;i<=r;i++)tt=min(tt,(double)(sum[i]+x)/(i+));
cout<<x-tt<<endl;
}
}
return ;
}

双指针:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pli pair<ll,int>
#define mem(a,b) memset(a,b,sizeof(a)) const int N=5e5+;
ll sum[N],cnt=,top=,x;
double cal(int l){
return (double)(x+sum[l])/(l+);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cout<<fixed<<setprecision();
int q,t;
cin>>q;
while(q--){
cin>>t;
if(t==){
cin>>x;
sum[++cnt]=sum[cnt-]+x;
}
else{
while(top<cnt){
if(cal(top+)<cal(top))top++;
else break;
}
cout<<x-cal(top)<<endl;
}
}
return ;
}

Codeforces 939E - Maximize!的更多相关文章

  1. codeforces 939E Maximize! 双指针(two pointers)

    E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  2. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  3. 2018.12.08 codeforces 939E. Maximize!(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...

  4. Codeforces 939E Maximize ( 三分 || 二分 )

    题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 :  首先关注 ...

  5. CodeForces 939E Maximize

    Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...

  6. codeforces#1139E. Maximize Mex(逆处理,二分匹配)

    题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...

  7. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  8. Codeforces Round #464 (Div. 2) E. Maximize!

    题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...

  9. Codeforces 939.E Maximize!

    E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. log4j配置目标到mongodb

    首先,具体采用什么技术作为集中式存储方案在99%的应用中应该来说并没有多大区别,最重要的是要定期清理不必要的日志,以及日志格式设计(也可以重写org.log4mongo.MongoDbPatternL ...

  2. android之进度条

    xml引用 <ProgressBar android:id="@+id/pb_progressbar" style="@style/StyleProgressBar ...

  3. mint-ui之tabbar使用

    <template> <div> <!-- tabcontainer --> <mt-tab-container class="page-tabba ...

  4. poj 2096 Collecting Bugs - 概率与期望 - 动态规划

    Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...

  5. Received empty response from Zabbix Agent at [172.16.1.7]...

    Centos7.5  zabbix添加主机发现ZBX爆红报错 原因:在配置/etc/zabbix/zabbix_agentd.conf中172.16.1.71写成了127.16.1.71 解决方法:重 ...

  6. python --- 11 第一类对象 函数名 闭包 迭代器

    一 .函数名的运用    ①函数名是⼀个变量, 但它是⼀个特殊的变量, 与括号配合可以执⾏函数的变量 ②函数名是一个内存地址    ③ 函数名可以赋值给其他变量         ④函数名可以当做容器类 ...

  7. ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(3)

    4. 主从表关系 名称 类型 说明 MasterSource 从表对应于主表的DataSource组件 DetailFields 从表中对应于主表字段的外键字段 MasterFields 主表中关联从 ...

  8. (转) The Incredible PyTorch

    转自:https://github.com/ritchieng/the-incredible-pytorch The Incredible PyTorch What is this? This is ...

  9. eclipse安装spring boot插件spring tool suite

    进行spring cloud的学习,要安装spring boot 的spring -tool-suite插件,我在第一次安装时,由于操作不当,两天才完全安装好,真的是要命了,感觉自己蠢死!下面就自己踩 ...

  10. 清除浏览器CSS样式

    /* YUI 3.18.1 (build f7e7bcb) Copyright 2014 Yahoo! Inc. All rights reserved. Licensed under the BSD ...