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 ...
随机推荐
- golang fatal error: all goroutines are asleep - deadlock!
转自:https://www.cnblogs.com/ghj1976/p/4295013.html http://blog.csdn.net/skh2015java/article/details/6 ...
- linux centos6.8搭建 jdk 环境
1. 上官网下载jdk1.8的包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- Lyft高管的技术团队管理实战
Lyft 的技术总监沈思维分享了他对于管理技术团队和打造工程文化的经验,也欢迎添加他的微信公众号"人家的屋顶"了解更多(微信公众号ID: othersroof).沈思维毕业于密歇根 ...
- PyTorch 使用心得
PyTorch 使用心得 模板 import torch.nn as nn import torch.optim as optim class Model(nn.Module): def __init ...
- 识别简单的答题卡(Bubble sheet multiple choice scanner and test grader using OMR, Python and OpenCV——jsxyhelu重新整编)
该博客转自www.pyimagesearch.com,进行了相关修改补充. Over the past few months I've gotten quite the number of reque ...
- (4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
1.联通ColaB 2.运行最基础mnist例子,并且打印图表结果 # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y gra ...
- ScheduledTheadPool线程池的使用
ScheduledTheadPool线程池的特点在于可以延迟执行任务,也可以周期性执行任务. 创建线程池 ScheduledExecutorService scheduled = Executors. ...
- ODAC(V9.5.15) 学习笔记(十一)TOraEncryptor、TOraPackage和TOraAlerter
TOraEncryptor 名称 类型 说明 DataHeader TCREncDataHeader 一些附加信息放入加密数据中,包括: ehNone 无附加信息 ehTag GUID和随机生成的 ...
- Super-palindrome 【可能是暴力】
Super-palindrome 时间限制: 1 Sec 内存限制: 128 MB 提交: 486 解决: 166 [提交] [状态] [命题人:admin] 题目描述 You are given ...
- Git学习笔记---安装与初始化 连接远程版本库
1.Git的安装 sudo apt-get install git 用的是linux(ubuntu)系统,安装非常简单,上面一条命令就够了. 2.初次运行的配置 Git 提供了一个叫做 git con ...