Codeforces Beta Round #5 E. Bindian Signalizing

题目大意

有N座山组成一个环,,两座山互相能看到的要求是相连的圆弧上没有任何其他的山高度比它们高。求能看到的山的组数。

解题思路

首先要拆环成链,将山的序列改变,第一座山是最高的山。 其次是统计对于这个序列的L数组和R数组。表示:

First hill to the left of the x, which is strictly higher than x. First hill to the right of the x, which is strictly higher than x.

生成C数组:

All hills that are as high as x and are located between x and y.

(来自官方题解)

最后统计答案。

题目代码;const int N = 1000005;int num[N],l[N],r[N],c[N],temp[N];int main(){int n,max1=-1,p=-1;scanf(“%d”,&n);for(int i=0; i<n; i++){scanf(“%d”,&temp[i]);if(max1<temp[i]){max1=temp[i];p=i;}}num[n]=temp[p];for(int i=0; i<n; i++){num[i]=temp[p++];p%=n;//cout<<num[i]<<” “;}for(int i=1;i<n;i++){l[i]=i-1;while(l[i]>0&&num[i]>=num[l[i]]){l[i]=l[l[i]];}}c[n]=0;for(int i=n-1;i>=0;i–){r[i]=i+1;while(r[i]<n&&num[i]>num[r[i]])r[i]=r[r[i]];if(r[i]<n&&num[i]==num[r[i]]){c[i]=c[r[i]]+1;r[i]=r[r[i]];}}long long ans=0;for(int i=1;i<n;i++){ans+=c[i];ans+=2;if(l[i]==0&&r[i]==n)ans–;}cout<<ans;return 0;}

他们不计后果的彼此拥抱,握紧双手,怕天会亮,怕爱会走。

Codeforces Beta Round #5 E. Bindian Signalizing

相关文章:

你感兴趣的文章:

标签云: