POJ 2018 斜率优化DP
2017-08-20 15:20
316 查看
题意
给一堆数,选不少于F个数的子序列,求均值最大的子序列。题解
最原始的斜率DP优化题目,最初出现在周源的国家队论文中。尽管这个题是最原始的题,但是这个题并不能用常用的套路。这个题的状态转移方程很明显,但是却不是标准的斜率优化方程(当然也差不多)。优化的话,基本上还是老套路,用一个单调队列进行优化。在选择最优元素的时候,对斜率进行判断,如果下一个点组成的斜率更大,那么将选择下一个元素。在插入元素的时候,也对斜率进行判断,保证斜率是单调递增的。注意事项
其实也没有太多需要注意的,主要就是这种题的一个老套路。循环要从F(限制大小)开始。原因也很简单,如果从1开始的话,那样就会产生个数小于F的子序列,从而可能导致平均数偏大。代码
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<string> #include<set> #include<map> #include<bitset> #include<stack> #include<string> #define UP(i,l,h) for(int i=l;i<h;i++) #define DOWN(i,h,l) for(int i=h-1;i>=l;i--) #define W(a) while(a) #define MEM(a,b) memset(a,b,sizeof(a)) #define LL long long #define INF 0x3f3f3f3f #define MAXN 800010 #define MOD 1000000009 #define EPS 1e-3 using namespace std; int sum[100010],q[100010]; double check(int a,int b){ return (double)(sum[b]-sum[a])/(b-a); } int main(){ // freopen("d://input.txt","r",stdin); int n,f; W(~scanf("%d%d",&n,&f)){ MEM(sum,0); UP(i,1,n+1){ int x; scanf("%d",&x); sum[i]=sum[i-1]+x; } int st=0,ed=0; q[ed++]=0; double ans=0; UP(i,f,n+1){ W(st+1<ed&&check(q[st],i)<=check(q[st+1],i)){ st++; } ans=max(ans,check(q[st],i)); // cout<<ans<<" "<<q[st]<<" "<<i<<endl; int k=i-f+1; W(st+1<ed&&check(q[ed-1],k)<=check(q[ed-2],q[ed-1])){ ed--; } q[ed++]=k; } printf("%d\n",int(ans*1000)); } }
相关文章推荐
- POJ 2018 Best Cow Fences + UVA Live 4726 Average 斜率优化DP
- POJ 2018 Best Cow Fences(斜率优化+DP)
- Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)
- poj 1260 Pearls 斜率优化dp
- [poj1260] Pearls DP斜率优化
- poj 1180 Batch Scheduling ( 斜率优化DP )
- poj 1260 Pearls 斜率优化dp
- POJ 3709 K-Anonymous Sequence(斜率优化DP)
- poj 1260 Pearls 斜率优化dp
- POJ 1180 Batch Scheduling (斜率优化DP)
- poj 3709 K-Anonymous Sequence 斜率优化dp
- POJ - 1180 Batch Scheduling(斜率优化DP)
- POJ 1180 斜率优化DP
- poj 1180 Batch Scheduling(斜率优化dp或决策单调)
- poj 2018 斜率优化(最大平均值问题)
- 斜率优化DP(POJ1180 && POJ3709)
- poj 1180:Batch Scheduling【斜率优化dp】
- poj 1180 Batch Scheduling 斜率优化dp
- POJ 2018 Best Cow Fences (斜率优化入门)
- hdu 2993 & POJ 2018 MAX Average Problem(斜率dp)