c_cpp HOJ - 302:最大平均值[AC]; http://hoj.twbbs.org/judge/judge/submission/21872

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp HOJ - 302:最大平均值[AC]; http://hoj.twbbs.org/judge/judge/submission/21872相关的知识,希望对你有一定的参考价值。

#define MAX 1000000
#define MULTI 1000
#include<cstdio>
#include<iostream>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> point;
int n,f;
ll sum[MAX+1];
deque<point> dq;

inline int crossProd(point a,point b, point c){
	return (b.first-a.first)*(c.second-b.second)-(b.second-a.second)*(c.first-b.first);
}

void push(point p){
	while(dq.size()>1){
		point m=dq[0];
		point o=dq[1];
		if(crossProd(o,m,p)<=0)dq.pop_front();
		else break;
	}
	dq.push_front(p);
}

bool canNext(point p){
	if(dq.size()<=1)return false;
	point a=dq.back();
	point b=*(dq.rbegin()+1);
	// avoid division
	// cross-multiplication instead
	return (p.second-a.second)*(p.first-b.first)<(p.second-b.second)*(p.first-a.first);
}

int main(){
	ll ans=0;
	scanf("%d%d",&n,&f);
	for(int i=1;i<=n;i++){
		ll ipt;
		scanf("%lld",&ipt);
		sum[i]=sum[i-1]+ipt*MULTI;
	}
	for(int i=f;i<=n;i++){
		push(make_pair(i-f,sum[i-f]));
		while(canNext(make_pair(i,sum[i])))dq.pop_back();
		point op=dq.back();
		ans=max(ans,(sum[i]-op.second)/(i-op.first));
	}
	printf("%lld\n",ans);
}

以上是关于c_cpp HOJ - 302:最大平均值[AC]; http://hoj.twbbs.org/judge/judge/submission/21872的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp HOJ 52瀚瀚数列@ 2013-02-18 20:14:18

搜索(DLX):HOJ 1017 - Exact cover

hoj Counting the algorithms

c_cpp 本本法的基本思想是在积分区间上随机均匀的产生点,即在[a,b]上随机均匀的取点,求出由这些点产生的函数值的算术平均值,再乘以区间宽度,即可解出定积分得近似解。

hoj 13792 Model Railroad

c_cpp 没有总和的计算平均值