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. spring使用@Autowired为抽象父类注入依赖

    有时候为了管理或者避免不一致性,希望具体服务统一继承抽象父类,同时使用@Autowired为抽象父类注入依赖.搜了了网上,有些解决方法实现实在不敢恭维,靠子类去注入依赖,那还有什么意义,如下: 父类: ...

  2. 11: Nginx安装lua支持

    1.1 Nginx 使用lua脚本 注:需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.Nginx安装lua支持 wget -c http ...

  3. JAVA中拆箱和装箱

    浅谈JAVA中拆箱与装箱 一.  什么是装箱?什么是拆箱? 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10) ...

  4. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  5. makefile基本操作

    多数内容copy自youtube的一个视频:https://www.youtube.com/watch?v=E1_uuFWibuM 执行环境:原作者是在Linux下做的视频,而我使用的是win10,w ...

  6. Pig项目&Spring Boot&Spring Cloud学习

    1.Spring条件加载原理(@Conditional,@ConditionalOnXXX注解) https://fangjian0423.github.io/2017/05/16/springboo ...

  7. sql -- 移除数据中的换行符和回车符

    https://blog.csdn.net/jcx5083761/article/details/40185795 --移除回车符 update master_location SET street_ ...

  8. 题解—— 洛谷 p1993 小K的农场(差分约束&负环判断)

    看到题就可以想到差分约束 判断负环要用dfs,bfs-spfa会TLE 4个点 bfs-spfa #include <cstdio> #include <algorithm> ...

  9. Graph Convolutional Networks (GCNs) 简介

    Graph Convolutional Networks 2018-01-16  19:35:17 this Tutorial comes from YouTube Video:https://www ...

  10. 【转载】谈谈自己对REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解

    转载自:https://blog.csdn.net/tantexian/article/details/48196453 SOA: 维基百科解释:SOA:面向服务的软件架构(Service Orien ...