标签:des c style class blog code
10 6 6 4 2 10 3 8 5 9 4 1
6.50
直接斜率DP:O(N)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <cmath> #include <map> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=100000+10; int n,k; int s[MAX],q[MAX]; double dp[MAX],sum[MAX]; double GetY(int i,int j){ return sum[i]-sum[j]; } int GetX(int i,int j){ return i-j; } double DP(){ int head=0,tail=1; q[head]=0; double ans=0; for(int i=1;i<=n;++i)sum[i]=sum[i-1]+s[i]*1.0; for(int i=k;i<=n;++i){ int j=i-k; while(head+1<tail && GetY(j,q[tail-1])*GetX(q[tail-1],q[tail-2])<=GetY(q[tail-1],q[tail-2])*GetX(j,q[tail-1]))--tail; q[tail++]=j; while(head+1<tail && GetY(i,q[head])*GetX(i,q[head+1])<=GetY(i,q[head+1])*GetX(i,q[head]))++head; dp[i]=(sum[i]-sum[q[head]])/(i-q[head]); ans=max(ans,dp[i]); } return ans; } int input(){//加速外挂 char ch=' '; int num=0; while(ch<'0' || ch>'9')ch=getchar(); while(ch>='0' && ch<='9')num=num*10+ch-'0',ch=getchar(); return num; } int main(){ while(~scanf("%d%d",&n,&k)){ for(int i=1;i<=n;++i)s[i]=input(); printf("%0.2lf\n",DP()); } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <cmath> #include <map> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=100000+10; int n,k; int s[MAX],q[MAX]; LL sum[MAX]; LL GetY(int i,int j){ return sum[i]-sum[j]; } int GetX(int i,int j){ return i-j; } LL check(int mid,int i){ return GetY(i,q[mid+1])*GetX(q[mid+1],q[mid])-GetY(q[mid+1],q[mid])*GetX(i,q[mid+1]); } int search(int l,int r,int i){ //由于斜率单调递增 /*int top=r; while(l<=r){//根据i与mid的斜率 和 i与mid+1的斜率之差求切点 if(l == r && l == top)return q[l];//这里一定要注意如果切点是最后一个点需要另判,因为mid+1不存在会出错 int mid=(l+r)>>1; if(check(mid,i)<0)r=mid-1; else l=mid+1; }*/ while(l<r){//根据i与mid的斜率 和 i与mid+1的斜率之差求切点 int mid=(l+r)>>1; if(check(mid,i)<0)r=mid; else l=mid+1; } return q[l]; } double DP(){ int head=0,tail=1,p; q[head]=0; double ans=0,dp; for(int i=1;i<=n;++i)sum[i]=sum[i-1]+s[i]; for(int i=k;i<=n;++i){ int j=i-k; while(head+1<tail && GetY(j,q[tail-1])*GetX(q[tail-1],q[tail-2])<=GetY(q[tail-1],q[tail-2])*GetX(j,q[tail-1]))--tail; q[tail++]=j; p=search(head,tail-1,i);//根据相邻点与i点的斜率之差二分查找切点 dp=(sum[i]-sum[p])*1.0/(i-p); if(dp>ans)ans=dp; } return ans; } int input(){//加速外挂 char ch=' '; int num=0; while(ch<'0' || ch>'9')ch=getchar(); while(ch>='0' && ch<='9')num=num*10+ch-'0',ch=getchar(); return num; } int main(){ while(~scanf("%d%d",&n,&k)){ for(int i=1;i<=n;++i)s[i]=input(); printf("%0.2lf\n",DP()); } return 0; }
hdu2993之斜率dp+二分查找,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://blog.csdn.net/xingyeyongheng/article/details/29644289