Codeforces 5E. Bindian Signalizing

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.

Sample test(s)
input
5
1 2 4 5 3
output
7

代码:

#include <stdio.h>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;

const int MAXN = 1000005;
int seq[MAXN];
int height[MAXN];

int main()
{
#ifdef _LOCAL
    freopen("F://input.txt", "r", stdin);
#endif
  int n, MAX = -1, id = 0;
  scanf("%d", &n);
  for(int i = 0; i < n; ++i)
  {
      scanf("%d", &seq[i]);
      if(seq[i] > MAX)
      {
          MAX = seq[i];
          id = i;
      }
  }
  for(int i = 0; i < n; ++i)
      height[i] = seq[(id + i) % n];
  long long int ans = 0;
  stack<pair<int, int>> stk;
  for(int i = 0; i < n ; ++i)
  {
      while(!stk.empty() && height[i] > stk.top().first)
      {
          ans += stk.top().second;
          stk.pop();
      }
      if(stk.empty())
          stk.push(make_pair(height[i], 1));
      else if(height[i] < stk.top().first)
      {
          ++ans;
          stk.push(make_pair(height[i], 1));
      }
      else if(height[i] == stk.top().first)
      {
          if(stk.size() != 1) ++ans;
          ans += stk.top().second;
          ++stk.top().second;
      }
  }
  while(stk.size() > 2)
  {
      ans += stk.top().second;
      stk.pop();
  }
  if(stk.size() == 2)
  {
      pair<int, int> temp = stk.top();
      stk.pop();
      if(stk.top().second > 1)
          ans += temp.second;
  }
  printf("%I64d", ans);
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值