这里是题目
这是暴力枚举方法。
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sum=n-1;
for(i=1;i<=n-2;i++)
{
int m=a[i+1];
for(j=i+1;j<=n-1;j++)
{
if(a[j]>a[i]) break;
else if(a[j+1]>=m)
sum++,m=a[j+1];
}
}
printf("%d",sum);
return 0;
这是单调栈的方法。
scanf("%d",&n);
for(i=1;i<=n;i++)
{
int x,f=1;
scanf("%d",&x);
if(x>=a[d])
{
while(a[d]<x&&d>=1)
{
d--;
sum++;
}
if(a[d]==x)
{
int j=d;
while(a[j]==x)
sum++,j--;
if(j!=0) sum++;
f=0;
}
if(d!=0&&f==1) sum++;
a[++d]=x;
}
else
{
sum++;
a[++d]=x;
}
}
printf("%d",sum);
return 0;
第一,刚开始连题都没读明白,233333,我认为两边的人有一个比中间的人高就可以算看见了,可实际上必须两个人都比中间的高才可以看见。语文不过关,还误导了hyc。QAQ。
第二,没有考虑到栈里没有人时总数是不用加的。这还算比较好想的,自己改了过来。
但是改了以后样例总是少一,到底是为何呢,那是因为我直接将与栈顶相等的情况直接归到了比栈顶大的情况。所以导致少了一个人,(样例真良心QAQ)。
but我还是忘了个问题,当相等时是不需要我已经总数加1了,而下面不相等时还有一个总数判断加一,所以导致多加了一个。
栈真是麻烦,还需要小心re 。