hdu2993
来源:互联网 发布:淘宝图片的像素是多少 编辑:程序博客网 时间:2024/06/10 15:52
这个题目是斜率dp,,,,,,在杭电oj一直都过不了,,,为嘛呢,,,我也不知道,,依稀大神的代码也都不能过,,,所以建议看看就好,,不做了
做这个题目的心得,,,这是一道很久以前就已经开发出来的题目奈何我的AC能力不够,只能看看大神的代码,,然后自己再写一下,,主要是维护下凸性,,这是这个题目提高效率的最好方法,,由于是单调的然后就可以用二分法求解出最大的斜率,其余细节自己还是要多多注意,,
本题好像只有TLE,,所以理解才是最重要的。。。
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#include<algorithm>using namespace std;const int MAXN=100010;int sum[MAXN];int q[MAXN];int top;long long cross(int a,int b,int c){ long long x1=b-a; long long y1=sum[b]-sum[a]; long long x2=c-b; long long y2=sum[c]-sum[b]; return x1*y2-y1*x2;}int bsearch(int l,int r,int i){ while(l<r) { int mid=(l+r)>>1; if(cross(q[mid],q[mid+1],i)<0)r=mid; else l=mid+1; } return l;}int input(){ char ch=' '; while(ch<'0'||ch>'9')ch=getchar(); int x=0; while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar(); return x;}int main(){ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n,k; while(scanf("%d%d",&n,&k)!=EOF) { top=0; sum[0]=0; for(int i=1;i<=n;i++) { sum[i]=input(); sum[i]+=sum[i-1]; } double ans=0; q[top++]=0; for(int i=k;i<=n;i++) { int j=i-k; while(top>1&&cross(q[top-2],q[top-1],j)<0)top--; q[top++]=j; int temp=bsearch(0,top-1,i); double f=((double)(sum[i]-sum[q[temp]]))/(i-q[temp]); if(f>ans)ans=f; } printf("%.2lf\n",ans); } return 0;}
0 0
- hdu2993
- hdu2993 斜率优化
- hdu2993 优先队列
- [HDU2993]MAX Average Problem
- 【DP+斜率优化】 hdu2993 MAX Average Problem
- hdu2993 MAX Average Problem(斜率+单调队列)
- hdu2993 MAX Average Problem (斜率dp)
- hdu2993之斜率dp+二分查找
- HDU2993 MAX average problem [斜率dp]
- [HDU2993] MAX Average Problem && 斜率优化问题
- HDU2993——MAX Average Problem(斜率优化DP)
- 读书笔记之c和指针(8)
- Linux网络编程(2)——采用TCP的基本server的实现
- 简单记录一下简单的理解
- 【Linux学习】epoll详解
- 随机查找数组中第i个元素(按顺序排列的)
- hdu2993
- 搜索引擎指令
- GDB调试程序(二)
- LeetCode(139) Word Break
- 读《UNIX网络编程 卷1:套接字联网API》[上]
- Clone Graph
- 20150809解题报告
- DNS挟持介绍
- HDU 5364-Distribution money(水题)