codeforces 5E(非原创)

E. Bindian Signalizing
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night.

In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the two hills there was no hill higher than any of the two. As for any two hills there are two different circle arcs connecting them, the signal was seen if the above mentioned condition was satisfied on at least one of the arcs. For example, for any two neighbouring watchmen it is true that the signal of one will be seen by the other.

An important characteristics of this watch system was the amount of pairs of watchmen able to see each other's signals. You are to find this amount by the given heights of the hills.

Input

The first line of the input data contains an integer number n (3 ≤ n ≤ 106), n — the amount of hills around the capital. The second line contains n numbers — heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109.

Output

Print the required amount of pairs.

Examples
Input
5
1 2 4 5 3
Output
7

(个人觉得这题的思路和5c很像,因为都是我想不出来的思路- -

题意:(题意是不可能读懂的
    让我队友帮我翻译了下,大致知道是给一个环,环上有n个点,如果每两个点之间没有比这两个点大的点,则这两个点能连一条线,问最多能连几条线。

解题思路:
    假设对于i,我们已经计算出了left[i], right[i], same[i],其中left[i]表示i左侧比第一个比i高的位置,right[i]表示i右侧第一个比i高的位置,same[i]表示从i到right[i]的左开右闭区间内高度等于i的山的数目。

     简而言之,left和right是位置,而same是数目。

     那么对于一座山i而言,它可以和left[i] 和 right[i]都构成能互相看见的pair,并且和i到right[i]之间所有高度等于i的山构成能互相看见的pair。

     所以问题就是计算left数组、right数组和same数组。(搜题解的

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 #define Max(a,b) a>b?a:b
 6 const int maxn=1000005;
 7 int a[maxn],b[maxn],lef[maxn],rig[maxn],c[maxn]={0};
 8 int main()
 9 {
10     int n;
11     scanf("%d",&n);
12     int ma=-1,mid=0;
13     for(int i=0;i<n;i++)
14     {
15         scanf("%d",&a[i]);
16         if(a[i]>ma)
17         {
18             ma=a[i];
19             mid=i;
20         }
21     }
22     mid--;
23     for(int j=1;j<=n;j++)
24         b[j]=a[(mid+j)%n];   //处理数组 
25     lef[1]=1;
26     for(int i=2;i<=n;i++)
27     {
28         lef[i]=i-1;
29         while(lef[i]>1&&b[lef[i]]<=b[i])
30             lef[i]=lef[lef[i]];//往左递归
31     }
32     for(int i=n;i>=1;i--)
33     {
34         rig[i]=i+1;
35         while(rig[i]<=n&&b[rig[i]]<b[i])
36             rig[i]=rig[rig[i]];//往右递归
37         if(rig[i]<=n&&b[rig[i]]==b[i])
38         {
39             c[i]=c[rig[i]]+1;
40             rig[i]=rig[rig[i]];
41         }
42     }
43     long long ans=0;
44     for(int i=2;i<=n;i++)
45     {
46         ans+=c[i]+2;
47         if(lef[i]==1&&rig[i]==n+1)
48             ans--;
49     }
50     printf("%lld\n",ans);
51 }
View Code

 

参考博客:http://blog.csdn.net/aholic/article/details/28155751

     http://blog.csdn.net/tree__water/article/details/52090499    

转载于:https://www.cnblogs.com/zmin/p/7712238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值