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 ...
随机推荐
- java Condition条件变量的通俗易懂解释、基本使用及注意点
最近在看pthread方面的书,看到条件变量一节的时候,回忆了下java中条件变量的使用方式. java中条件变量都实现了java.util.concurrent.locks.Condition接口, ...
- DevExpress GridControl使用方法总结2
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...
- Unity3D之主菜单
1.新建一个名为MainMenu的C#脚本,修改编码后拖动到主摄像机,并给主摄像机添加一个AudioSource声音源作为背景音乐.将音乐文件赋值给Audio Clip属性. 2.创建一个Common ...
- Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
centos7.5 安装mysql数据库报错 问题: [root@db04-54 scripts]# /etc/init.d/mysqld start /etc/init.d/mysqld: line ...
- 软件调用QML的两种方式
一.两种方式 二.方式1[对窗口的控制权在QML] 三.方式2[对窗口的控制权在C++]
- topcoder srm 702 div1 -3
1.给定一个$n*m$的矩阵,里面的数字是1到$n*m$的一个排列.一个$n*m$矩阵$A$对应一个$n*m$ 的01字符串,字符串的位置$i*m+j$为1当且仅当$A_{i,j}=i*m+j+1$. ...
- imx6ul linux4.1.15 LED驱动配置及heartbeat源码分析【转】
本文转载自:https://blog.csdn.net/u010444107/article/details/78328807 1)查看内核配置wujun@wj-vBox:~/freescale/li ...
- 编译openwrt时报错:fstools-2018-01-02-11efbf3b/libfstools/overlay.c: At top level: cc1: error: unrecognized command line option '-Wno-format-truncation' [-Werror]
1. 详细错误信息 [ 11%] Building C object CMakeFiles/fstools.dir/libfstools/overlay.c.o/home/jello/openwrt/ ...
- tp框架中的一些疑点知识-1
tp默认的编码是utf-8 Runtime中的Cache和Logs都是分模块的,因为在应用app下可以有多个模块,但是 公共模块和Runtime模块只有一个, 所以, Runtime要包含各个模块的内 ...
- JXOI2018守卫 区间DP
链接 https://loj.ac/problem/2545 思路 f[i][j]表示i到j区间的最小监视人数 可以预处理出来g[i][j],表示i能否监视到j (其实预处理的关系不大,完全可以直接判 ...