【斜率优化】HDU 2993 MAX Average Problem

通道

题意:求出长度大于k子序列使得其各个元素之和的平均数最大,并输出最大平均值

思路:浅谈数形结合思想在信息学竞赛中的应用

代码:

#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;
}

时间: 2024-11-06 03:32:30

【斜率优化】HDU 2993 MAX Average Problem的相关文章

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

hdu 2993 MAX Average Problem(斜率DP入门题)

题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训队论文<周源--浅谈数形结合思想在信息学竞赛中的应用> 这题输入有点大,要加读入优化才能过. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5

HDU 2993 MAX Average Problem(斜率优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 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

HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的平均值最小. 解题思路:斜率DP经典题, 详细分析见: NOI2004年周源的论文<浅谈数形结合思想在信息学竞赛中的应用> 还有要注意要用输入输出外挂,不是getchar()版的,是fread()版的,第一次遇到这么变态的题目- -|||. 代码: 1 #include<iostream&g

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

BNUOJ 3958 MAX Average Problem

MAX Average Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average v

hdu 3480 Division (斜率优化)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 2676    Accepted Submission(s): 1056 Problem Description Little D is really interested in the theorem of sets recently. There's a pro

HDU 3480 Division(斜率优化+二维DP)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 3984    Accepted Submission(s): 1527 Problem Description Little D is really interested in the theorem of sets recently. There’s a pro

HDU 3507 Print Article 斜率优化

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4810    Accepted Submission(s): 1451 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique