当前位置:网站首页>E - Average and Median(二分)
E - Average and Median(二分)
2022-06-24 22:57:00 【Harris-H】
E - Average and Median(二分)
第一问二分答案 x x x,然后可以所有数 − x -x −x,那么就是在要求下满足 ∑ ( a i − x ) ≥ 0 \sum (a_i-x)\ge 0 ∑(ai−x)≥0
可以dp。
第二问同理,二分答案 x x x,然后 b i = [ a i ≥ x ] b_i = [a_i\ge x] bi=[ai≥x]
那么就是 ∑ b i ≥ 0 \sum b_i\ge 0 ∑bi≥0
也是dp。
时间复杂度: O ( n l o g V ) O(nlogV) O(nlogV)
难点就是在于一开始没想到 二分后怎么快速算。原来可以dp。
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod 1000000007
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
int n, m, k;
int tr[MAX];
double dp[MAX][2];
bool check1(double mid){
dp[0][0] = dp[0][1] = 0.0;
for(int i = 1; i <= n; ++i){
dp[i][0] = dp[i - 1][1];
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + tr[i] - mid;
}
if(max(dp[n][1], dp[n][0]) >= 0)return true;
else return false;
}
bool check2(int mid){
dp[0][0] = dp[0][1] = 0;
for(int i = 1; i <= n; ++i){
dp[i][0] = dp[i - 1][1];
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + (tr[i] >= mid ? 1 : -1);
}
if(max(dp[n][1], dp[n][0]) > 0)return true;
else return false;
}
void work(){
cin >> n;
for(int i = 1; i <= n; ++i)cin >> tr[i];
double l = 0, r = 1000000000.0;
while (r - l >= 1e-6) {
double mid = (l + r) / 2;
if(check1(mid))l = mid;
else r = mid;
}
int ll = 0, rr = 1000000000;
while (ll <= rr) {
int mid = (ll + rr) / 2;
if(check2(mid))ll = mid + 1;
else rr = mid - 1;
}
cout << r << '\n' << rr << endl;
}
int main(){
io;
work();
return 0;
}
边栏推荐
- Use of hashcat
- Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
- Dataease template market officially released
- Intranet learning notes (6)
- 探索C语言程序奥秘——C语言程序编译与预处理
- 【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
- 3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
- 华为、阿里等大厂程序员真的好找对象吗?
- Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?
猜你喜欢

Test / development programmers, 30, do you feel confused? And where to go

How to monitor the log through the easycvr interface to observe the platform streaming?

左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全

当他们在私域里,掌握了分寸感

The ecosystem of the yuan universe

Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety

3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?

Intranet learning notes (7)

常用的软件测试工具清单,请查收。

做软件安全测试的作用,如何寻找软件安全测试公司出具报告?
随机推荐
一线城市软件测试工资——你拖后腿了吗
业务与技术双向结合构建银行数据安全管理体系
Exploring the mystery of C language program -- C language program compilation and preprocessing
MeterSphere開源持續測試平臺與阿裏雲雲效DevOps的集成
How to get the picture outside the chain - Netease photo album [easy to understand]
进入阿里做测试员遥不可及?这里或许有你想要的答案
内网学习笔记(7)
【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
Taishan Office Technology Lecture: a simple study of Chinese punctuation in vertical arrangement
实战攻防演练中的四大特点
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
产业互联网的概念里有「互联网」字眼,但却是一个和互联网并不关联的存在
保险APP适老化服务评测分析2022第06期
How do the TMUX color palette work?
基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类
【STL源码剖析】配置器(待补充)
Post competition summary of kaggle patent matching competition
How to monitor the log through the easycvr interface to observe the platform streaming?
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
2022年云计算应用关键威胁调查