当前位置:网站首页>Two point answer, 01 score planning (mean / median conversion), DP
Two point answer, 01 score planning (mean / median conversion), DP
2022-06-26 14:23:00 【Honestbutter-】
Topic link
Solution to the problem
Add :
- About the average 01 Planning understanding :
∑ a i n ≥ m i d \frac{\sum{a_i}}{n}≥mid n∑ai≥mid
∑ a i ∑ i = 1 n 1 ≥ m i d \frac{\sum{a_i}}{\sum_{i=1}^n1}≥mid ∑i=1n1∑ai≥mid
∑ ( a i − m i d ) ≥ 0 \sum{(a_i-mid)}≥0 ∑(ai−mid)≥0
- Also note that the median is an integer , The average is a floating point number
- We know that for a certain location i i i, You can choose this number , You can also choose his next number , Subconsciously want to use DFS, But the data is too big , Today I know I can use DP Before dynamic planning is selected i i i The maximum number .
- DP Thinking from “ From which position does a certain position move ” consider , The array represents the pre selection i i i The maximum number .
#include<iostream>
using namespace std;
const int N=1e5+100;
double a[N],b[N],dp[N][2];
int n;
bool check1(double mid)
{
for(int i=1;i<=n;i++) b[i]=a[i]-mid;
for(int i=1;i<=n;i++)
{
dp[i][0]=dp[i-1][1];
dp[i][1]=max(dp[i-1][1],dp[i-1][0])+b[i];
}
return max(dp[n][0],dp[n][1])>=0;
}
bool check2(int mid)
{
for(int i=1;i<=n;i++) b[i]=(a[i]>=mid)? 1:-1;
for(int i=1;i<=n;i++)
{
dp[i][0]=dp[i-1][1];
dp[i][1]=max(dp[i-1][1],dp[i-1][0])+b[i];
}
return max(dp[n][0],dp[n][1])>0;
}
int f2()
{
int l=0,r=0x3f3f3f3f; // The median of two
while(l<r)
{
int mid=(l+r+1)/2;
if(check2(mid)) l=mid;
else r=mid-1;
}
return l;
}
double f1() // Two point average
{
double l=0,r=0x3f3f3f3f;
while(r-l>1e-6)
{
double mid=(l+r)/2;
if(check1(mid)) l=mid;
else r=mid;
}
return l;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cout<<f1()<<endl<<f2()<<endl;
return 0;
}
边栏推荐
- How to check if a text field is empty or not in swift
- ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
- Hard (magnetic) disk (I)
- [wc2006] director of water management
- 量化框架backtrader之一文读懂observer观测器
- Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
- New specification of risc-v chip architecture
- Insect operator overloaded a fun
- CF676C Vasya and String
- Intellij IDEA--格式化SQL文件的方法
猜你喜欢

One article of the quantification framework backtrader read observer

7.consul service registration and discovery

Sword finger offer 05.58 Ⅱ string

Common controls and custom controls

Sword finger offer 21.57.58 I Double pointer (simple)

Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)

9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions

windows版MySQL软件的安装与卸载

RISC-V 芯片架构新规范

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
随机推荐
服务器创建虚拟环境跑代码
BP neural network for prediction
[jsoi2015] string tree
Luogu p4513 xiaobaiguang Park
Common operation and Principle Exploration of stream
Introduction to granular computing
First k large XOR value problem
C语言基础知识入门(大全)「建议收藏」
Common evaluation indexes of classification model -- confusion matrix and ROC curve
登录认证服务
Free machine learning dataset website (6300+ dataset)
Sword finger offer 06.24.35 Linked list
transformers DataCollatorWithPadding类
Is it safe to open a securities account? Is there any danger
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
Build your own PE manually from winpe of ADK
Comparison of disk partition modes (MBR and GPT)
MySQL | basic commands
Self created notes (unique in the whole network, continuously updated)
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》