标签:des style blog http java color
10 6 6 4 2 10 3 8 5 9 4 1
6.50
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 100005 #define MAXN 200005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; int n,m,ans,cnt,tot,flag; int a[maxn],sum[maxn]; int q[maxn]; double res; void solve() { int i,j,t,cur; int head=0,tail=-1; double pre,dx1,dy1,dx2,dy2; res=0; for(i=m; i<=n; i++) { cur=i-m; while(head<tail) { dy1=sum[cur]-sum[q[tail]]; dx1=cur-q[tail]; dy2=sum[q[tail]]-sum[q[tail-1]]; dx2=q[tail]-q[tail-1]; if(dy1*dx2<=dx1*dy2) tail--; else break ; } q[++tail]=cur; while(head<tail) { dy1=sum[i]-sum[q[head]]; dx1=i-q[head]; dy2=sum[i]-sum[q[head+1]]; dx2=i-q[head+1]; if(dy1*dx2<=dx1*dy2) head++; else break ; } res=max(res,(sum[i]-sum[q[head]]+0.0)/(i-q[head]+0.0)); // 不能写dy1/dx1 上一个循环可能不进入 } printf("%.2f\n",res); } int main() { int i,j,t; while(~scanf("%d%d",&n,&m)) { sum[0]=0; for(i=1; i<=n; i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } solve(); } return 0; }
hdu 2993 MAX Average Problem (斜率优化dp入门),布布扣,bubuko.com
hdu 2993 MAX Average Problem (斜率优化dp入门)
标签:des style blog http java color
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/36176189