当前位置:网站首页>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;
}
边栏推荐
- 常用的软件测试工具清单,请查收。
- qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
- write a number of lines to a new file in vim
- Convert string array to list collection
- 当他们在私域里,掌握了分寸感
- 探索C语言程序奥秘——C语言程序编译与预处理
- [mobile terminal] design size of mobile phone interface
- 如何选择正规安全的外汇交易平台?
- [day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
- Qt中使用QDomDocument操作XML文件
猜你喜欢

Chinese address and English address

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

yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本

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

When they are in private, they have a sense of propriety

I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating

Please run IDA with elevated permissons for local debugging.

进入阿里做测试员遥不可及?这里或许有你想要的答案

都2022年了,你还不了解什么是性能测试?

疫情防控,居家办公,网上授课之心得 | 社区征文
随机推荐
02 common codes for Epicor secondary development
Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
Kaggle 专利匹配比赛金牌方案赛后总结
Investigation on key threats of cloud computing applications in 2022
非凸联合创始人李佐凡:将量化作为自己的终身事业
【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
1-6搭建Win7虚拟机环境
File system - basic knowledge of disk and detailed introduction to FAT32 file system
Can automate - 10k, can automate - 20K, do you understand automated testing?
【第26天】给定 n 个元素的升序数组nums,求实现一个函数在nums中寻找target的下标 | 初识二分查找
如何卸载cuda
会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?
Smartctl 打开设备遇到 Permission denied 问题排查过程记录
多模态情感识别_多模态融合的情感识别研究「建议收藏」
泰山OFFICE技术讲座:竖排时中文标点的简单研究
Do you know your ABC
数据库系统概论必背知识
mysql命令备份
2022年云计算应用关键威胁调查
It's 2022, and you still don't know what performance testing is?