您的位置:首页 > 其它

2016"百度之星" - 初赛(Astar Round2B)

2016-05-22 16:48 267 查看
1001 区间的价值:


   RMQ+扫描法

  我们预处理RMQ求任意区间的最大值

  预处理出以a[i]为最小值 能向左延伸 向右延伸的 L[i], R[i]

  那么对于 一个答案 (L[i], R[i]) *rmq(L[i],R[i]) 为此长度的答案,我们可以发现他是可以更新到小于其长度的所有长度答案的,更新就好

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<map>
using namespace std;
const int N = 1e5+20, M = 1e4, mod = 1000000007,inf = 1e9;
typedef long long ll;

int a
,b
,n,now
;
int H
;
ll ans
;
int  f
,h
;

int main() {
while(scanf("%d",&n)!=EOF) {
for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i] = a[i];
for(int i=1;i<=n;i+=1) {

memset(f,0,sizeof(f));
memset(h,0,sizeof(h));

for(int j=1;j<=n;j++)
if(a[i]==a[j]) now[j] = 0;
else if(a[i]>a[j]) now[j] = 1;
else if(a[i]<a[j]) now[j] = -1;

int last = 0,mx = 0;
ll aa = 1;
for(int j=i-1;j>=1;j--) f[last+now[j]+M]++,last +=now[j],mx = max(mx,last);
last = 0;
for(int j=i+1;j<=n;j++) h[last+now[j]+M]++,last +=now[j],mx = max(mx,last);
for(int j=1;j<=mx;j++) aa += ((ll)f[j+M]*(ll)h[-j+M]),aa+=((ll)f[-j+M]*(ll)h[j+M]);
aa+=f[0+M];
aa+=h[0+M];
aa+=(ll)f[0+M]*(ll)h[0+M];
ans[i] = aa;
}
for(int i=1;i<n;i+=1) {
printf("%I64d ",ans[i]);
}
printf("%I64d\n",ans
);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: