E. Bindian Signalizing

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/5/E

Description

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 Input

5
1 2 4 5 3

Sample Output

7

HINT

题意

给你一个环的山,如果两个山之间没有比这座山更高的山的话,那么这两座山是可以互相看见的,然后问你最后有多少座山可以互相看见

题解:

首先我们把环变成一个链,然后用一个并查集的思想,处理每一座山左边第一个比这个山高的山是哪个,右边第一个比这个山高的是哪个,这个区域内和这个山高度一样的山有多少

然后跑一发并查集就好了

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000010
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff;
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int a[maxn];
int b[maxn];
int l[maxn];
int r[maxn];
int k[maxn];
int main()
{
int n=read();
int p=-;
for(int i=;i<n;i++)
{
a[i]=read();
if(p==-)
p=i;
else if(a[i]>a[p])
{
p=i;
}
}
for(int i=;i<n;i++)
b[i]=a[(i+p+n)%n];
b[n]=a[p];
for(int i=;i<n;i++)
{
l[i]=i-;
while(l[i]>&&b[i]>=b[l[i]])
l[i]=l[l[i]];
}
k[n]=;
for(int i=n-;i>=;i--)
{
r[i]=i+;
while(r[i]<n&&b[i]>b[r[i]])
r[i]=r[r[i]];
if(r[i]<n&&b[i]==b[r[i]])
{
k[i]=k[r[i]]+;
r[i]=r[r[i]];
}
}
ll ans=;
for(int i=;i<n;i++)
{
ans+=k[i];
ans+=;
if(l[i]!=||r[i]!=n)
ans++;
}
cout<<ans<<endl;
}

Codeforces Beta Round #5 E. Bindian Signalizing 并查集的更多相关文章

  1. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  2. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. 1、android源代码下载与跟踪

     学习Android源代码的目的 理解Android API查找API(Activity.Content Provider等) 高级应用开发(ROM定制)  在不同平台下载Android源代码 W ...

  2. 【Mongo】MongoVUE的使用

    我一直是在终端操作的mongo,各种update set remove insert,后来查了下有个叫mongoVUE的客户端,界面化比较方便操作吧~ 网上搜了下,说MongoVUE已经n年没有更新了 ...

  3. 单机版搭建Hadoop环境图文教程详解

    安装过程: 一.安装Linux操作系统二.在Ubuntu下创建hadoop用户组和用户三.在Ubuntu下安装JDK四.修改机器名五.安装ssh服务六.建立ssh无密码登录本机七.安装hadoop八. ...

  4. bzoj3620 似乎在梦中见过的样子

    好久没有写过KMP了,今天写个KMP练练手.此题就是枚举左端点暴力,用KMP做到O(n^2) #include<cstdio> #include<cstring> using ...

  5. C# 实现无焦点窗体(转载)

    #region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")] private extern stat ...

  6. 机器学习&数据挖掘笔记(常见面试之机器学习算法思想简单梳理)

    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 前言: 找工作时( ...

  7. 凸包模板 POJ1873

    // 凸包模板 POJ1873 // n=15所以可以按位枚举求凸包,再记录数据 #include <iostream> #include <cstdio> #include ...

  8. URAL-1997 Those are not the droids you're looking for 二分匹配

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 题意:记录了n个人进出门的时间点,每个人在房子里面待的时间要么小于等于a,要么大于 ...

  9. 版本控制:SVN中Branch/tag的使用 -摘自网络

    在SVN中Branch/tag在一个功能选项中,在使用中也往往产生混淆. 在实现上,branch和tag,对于svn都是使用copy实现的,所以他们在默认的权限上和一般的目录没有区别.至于何时用tag ...

  10. quora 中有关angular与emberjs的精彩辩论

    原贴地址,要注册才能看,这里只有国人翻译的一部分内容 本文源自于Quora网站的一个问题,作者称最近一直在为一个新的Rails项目寻找一个JavaScript框架,通过筛选,最终纠结于Angular. ...