Created
June 9, 2014 14:31
-
-
Save andy0130tw/e9fdc4491ba1873e3917 to your computer and use it in GitHub Desktop.
HOJ - 302: 最大平均值 [AC]; http://hoj.twbbs.org/judge/judge/submission/21872
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment