hdoj MAX Average Problem 2993 (斜率优化DP)

211 篇文章 1 订阅
85 篇文章 0 订阅

MAX Average Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7613    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
一种新思想。。。
//看网上的都是TLM。。。先贴上。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 100010
using namespace std;
int sum[N],s[N],a[N];
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 fx(int i,int t)
{
	double tmp;
	tmp=1.0*(sum[t]-sum[i])/(t-i);
	return tmp;
}
int main()
{
	int n,k;
	int i;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		sum[0]=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum[i]=sum[i-1]+a[i];
		}
		int top=0,low=0;
		double ans=0;
		for(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&&fx(s[low+1],i)>=fx(s[low],i))
				low++;
			ans=max(ans,fx(s[low],i));
		}
		printf("%.2lf\n",ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值