Codeforces 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!的更多相关文章
- codeforces 939E Maximize! 双指针(two pointers)
E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- 2018.12.08 codeforces 939E. Maximize!(二分答案)
传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...
- Codeforces 939E Maximize ( 三分 || 二分 )
题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 : 首先关注 ...
- CodeForces 939E Maximize
Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...
- codeforces#1139E. Maximize Mex(逆处理,二分匹配)
题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...
- Codeforces 1139E Maximize Mex 二分图匹配
Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...
- Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...
- Codeforces 939.E Maximize!
E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- linux释放页面缓存drop_caches
关于drop_caches文件:系统默认为0 在Documentation/sysctl/vm.txt中有如下描述: drop_caches Writing to this will cause th ...
- ORA-16038 ORA-19809 ORA-00312
问题表现: 连接数据库启动报错,ORA-03113, 查看详细的alert日志发现更多报错,如下 ORA-19809: 超出了恢复文件数的限制ORA-19804: 无法回收 209715200 字节磁 ...
- 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 解决方法:重 ...
- Python3 tkinter基础 Label justify 多行字符串左对齐
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- P3178 [HAOI2015]树上操作
P3178 [HAOI2015]树上操作 思路 板子嘛,其实我感觉树剖没啥脑子 就是debug 代码 #include <bits/stdc++.h> #define int long l ...
- 颠倒的价牌|2013年蓝桥杯A组题解析第四题-fishers
颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店. 其标价都是4位数字(即千元不等). 小李为了标价清晰.方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参 ...
- hihoCoder week20 线段树的区间修改
区间修改 区间查询 最后一场比赛前的无可救药的热身 #include <bits/stdc++.h> using namespace std; #define mid ((l+r)/2) ...
- SQL 多行合并一行
select stuff((select ',' + CONVERT(VARCHAR(50),id)+'' from tab_menu group by id for xml path('')), ...
- 【Hadoop 分布式部署 十:配置HDFS 的HA、启动HA中的各个守护进程】
官方参考 配置 地址 :http://hadoop.apache.org/docs/r2.5.2/hadoop-project-dist/hadoop-hdfs/HDFSHighAvailabili ...
- Docker:Containers
Prerequisites Install Docker version 1.13 or higher. Read the orientation in Part 1. Give your envir ...