当前位置:网站首页>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;
}
边栏推荐
- 文件系统 -- 磁盘基础知识和FAT32文件系统详细介绍
- Logminer database log mining
- 元宇宙的生态圈
- Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
- Experience of epidemic prevention and control, home office and online teaching | community essay solicitation
- beescms网站渗透测试和修复意见「建议收藏」
- DDD概念复杂难懂,实际落地如何设计代码实现模型?
- Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?
- 实战攻防演练中的四大特点
猜你喜欢

Folding screen will become an important weapon for domestic mobile phones to share the apple market

入职一家新公司,如何快速熟悉代码?

3年测试经验,连简历上真正需要什么都没搞明白,张口就要20k?

一线城市软件测试工资——你拖后腿了吗

做软件安全测试的作用,如何寻找软件安全测试公司出具报告?

Please run IDA with elevated permissons for local debugging.

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

Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?

AI服装生成,帮你完成服装设计的最后一步
Cusdis - lightweight, privacy first open source comment system | chain of the city
随机推荐
Intranet learning notes (6)
实战攻防演练中的四大特点
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
Basic layout -qhboxlayout class, qvboxlayout class, qgridlayout class
折叠屏将成国产手机分食苹果市场的重要武器
vim的Dirvish中文文档
How can Huatai Securities open an account to achieve one in ten thousand? Are securities accounts safe and reliable
Android Internet of things application development (smart Park) - set sensor threshold dialog interface
调用系统函数安全方案
疫情防控,居家办公,网上授课之心得 | 社区征文
ida中交叉引用的解析
|How to analyze bugs? Professional summary and analysis
EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?
【Proteus仿真】Arduino UNO+继电器控制照明设备
yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
测试/开发程序员,30而立,你是否觉得迷茫?又当何去何从......
非凸联合创始人李佐凡:将量化作为自己的终身事业
内网学习笔记(5)
Redistemplate operates redis. This article is enough (I) [easy to understand]
Please run IDA with elevated permissons for local debugging.