当前位置:网站首页>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;
}
边栏推荐
- 布局管理器~登录界面的搭建实例
- Hands on data analysis unit 3 model building and evaluation
- Introduction to granular computing
- Sword finger offer 18.22.25.52 Double pointer (simple)
- Pycharm远程连接服务器来跑代码
- Practice with the topic of bit operation force deduction
- hands-on-data-analysis 第三单元 模型搭建和评估
- D check type is pointer
- Knowledge about adsorption
- CF676C Vasya and String
猜你喜欢
How to call self written functions in MATLAB
Eigen(3):error: ‘Eigen’ has not been declared
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Pycharm远程连接服务器来跑代码
BP neural network for prediction
Common operation and Principle Exploration of stream
Use performance to see what the browser is doing
9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
Build your own PE manually from winpe of ADK
C language | Consortium
随机推荐
ArcGIS secondary development method - layer related operations (add, modify)
启动Redis报错:Could not create Server TCP listening socket *:6379: bind: Address already in use–解决办法
PostGIS create spatial database
Hands on data analysis unit 3 model building and evaluation
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
BP neural network for prediction
FreeFileSync 文件夹比较与同步软件
Insect operator overloaded a fun
常用控件及自定义控件
Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
wptx64能卸载吗_win10自带的软件哪些可以卸载
STM32F1和GD32F1有什么区别?
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
Sword finger offer 05.58 Ⅱ string
Eigen(3):error: ‘Eigen’ has not been declared
C语言基础知识入门(大全)「建议收藏」
Flex & Bison 开始
Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)
Niuke challenge 53:c. strange magic array
ThreadLocal巨坑!内存泄露只是小儿科...