zoukankan      html  css  js  c++  java
  • MAX Average Problem(斜率优化dp)

    MAX Average Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7574    Accepted Submission(s): 1667

    Problem Description
    Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.
     
    Input
    There multiple test cases in the input, each test case contains two lines. The first line has two integers, N and k (k<=N<=10^5). The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
     
    Output
    For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.
     
    Sample Input
    10 6 6 4 2 10 3 8 5 9 4 1
     
    Sample Output
    6.50
     

    题解:

    http://www.docin.com/p-47950655.html这篇论文讲的斜率优化,讲的很清楚;

    给定一个长度为n的序列,从其中找连续的长度大于m的子序列使得子序列中的平均值最小。

    总结就是:构造下凸折线,维护下凸折线(凸包维护),找与下凸折线相切的斜率(也可以用二分来找)

    没用二分:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define SD(x) scanf("%lf",&x)
    #define P_ printf(" ")
    typedef long long LL;
    const int MAXN=1e5+100;
    int sum[MAXN],s[MAXN],a[MAXN];
    bool cross(int i,int j,int k){
        if((sum[j]-sum[i])*(k-i)>=(sum[k]-sum[i])*(j-i))return true;
        return false;
    }
    double flx(int i,int t){
        double temp;
        temp=1.0*(sum[t]-sum[i])/(t-i);
        return temp;
    }
    int main(){
        int N,k;
        while(~scanf("%d%d",&N,&k)){
            sum[0]=0;
            for(int i=1;i<=N;i++)SI(a[i]),sum[i]=sum[i-1]+a[i];
            int top=0,low=0;
            double ans=0;
            for(int i=k;i<=N;i++){
                int j=i-k;
                while(top-low>=1&&cross(s[top-1],s[top],j))top--;
                    s[++top]=j;
                while(top-low>=1&&flx(s[low+1],i)>=flx(s[low],i))low++;
                ans=max(ans,flx(s[low],i));
            }
            printf("%.2lf
    ",ans);
        }
        return 0;
    }

    用了二分:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define SD(x) scanf("%lf",&x)
    #define P_ printf(" ")
    typedef long long LL;
    const int MAXN=1e5+100;
    int sum[MAXN],s[MAXN],a[MAXN];
    bool cross(int i,int j,int k){
        if((sum[j]-sum[i])*(k-i)>=(sum[k]-sum[i])*(j-i))return true;
        return false;
    }
    double flx(int i,int t){
        double temp;
        temp=1.0*(sum[t]-sum[i])/(t-i);
        return temp;
    }
    int erfen(int l,int r,int i){
        int mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(cross(s[mid],s[mid+1],i))r=mid-1;
            else l=mid+1;
        }
        return r+1;
    }
    int main(){
        int N,k;
        while(~scanf("%d%d",&N,&k)){
            sum[0]=0;
            for(int i=1;i<=N;i++)SI(a[i]),sum[i]=sum[i-1]+a[i];
            int top=0,low=0;
            double ans=0;
            for(int i=k;i<=N;i++){
                int j=i-k;
                while(top-low>=1&&cross(s[top-1],s[top],j))top--;
                    s[++top]=j;
                ans=max(ans,flx(s[erfen(0,top,i)],i));
            }
            printf("%.2lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    布局重用 include merge ViewStub
    AS 常用插件 MD
    AS 2.0新功能 Instant Run
    AS .ignore插件 忽略文件
    AS Gradle构建工具与Android plugin插件【大全】
    如何开通www国际域名个人网站
    倒计时实现方案总结 Timer Handler
    AS 进行单元测试
    RxJava 设计理念 观察者模式 Observable lambdas MD
    retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5205831.html
Copyright © 2011-2022 走看看