這個(gè)題解很好,YM啊
http://hi.baidu.com/oichampion/blog/item/ed51694a0e1256d9d1c86ae4.html
這個(gè)題目WA了一下午了,哎哎,得研究研究單調(diào)棧和單調(diào)隊(duì)列去!
總結(jié):多發(fā)現(xiàn)問題的本質(zhì)。
數(shù)學(xué)本質(zhì)!!!
#include<stdio.h>
#include<string.h>
#include<math.h>
long long stack[80008];
int main()
{
int i,n,top;
long long x,ans;
while (scanf("%d",&n)==1)
{
top=0;ans=0;
for (i=1; i<=n ; i++ )
{
scanf("%I64d",&x);
while (top&&x>=stack[top])
top--;
ans+=top;
top++;
stack[top]=x;
}
printf("%I64d\n",ans);
}
return 0;
}