Codeforces Beta Round #5 E. Bindian Signalizing 并查集
E. Bindian Signalizing
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/problemset/problem/5/E
Description
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 并查集的更多相关文章
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- 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]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- ajax轮询
oa.comet = function (id) { if (oa.id == 0) oa.id = id; $.ajax({ url: '/comet.asy?id=' + ...
- [讲座]【项目收集】“清流资本”互联网金融沙龙——颠覆者的创新与机会
[项目收集]"清流资本"互联网金融沙龙--颠覆者的创新与机会 2014年4月19日 14:00 - 2014年4月19日 17:00 北京海淀北京海淀区海淀图书城南侧3W咖啡 限额 ...
- GitHub使用教程及常见错误解决
1.下载Git并安装 Git for Windows Git-1.8.4-preview20130916.exe 按照默认步骤完成安装 2.设置SSH建立计算机与Github的链接 2.1 点击 开始 ...
- SSD Cloud Hosting - Linode的配置和部署,搭建Java环境
0.发牢骚 前一个月在淘宝购买了个Jsp空间,挺便宜的,才38元/年.部署了程序,然后ALIMAMA验证网站,一直提示验证失败.最后找卖家,他说可能是因为空间太慢,照他的推荐换了最好的空间,138元/ ...
- css3.0新属性效果在ie下的解决方案(兼容性)
css3.0增加的新属性,如投影.渐变.旋转.圆角等等!这些新标准属性在ie6.ie7.ie8浏览器版本里得不到很好的支持,相信ie以后的新版本也会支持这些新属性的.目前ie6.ie7.ie8浏览器不 ...
- [原创]Devexpress XtraReports 系列 2 创建表格报表
昨天发表了Devexpress XtraReports系列开篇,今天我们继续. 今天的主题是创建表格报表. 首先我们来看看最后实现的效果.Demo最后附上. 接下来开始讲解如何一步一步做出这个报表: ...
- Struts – Multiple configuration files example
Many developers like to put all Struts related stuff (action, form) into a single Struts configurati ...
- 关于Windows系统防火墙
步入win7时代,一般用户,真的没必要再去找墙了,系统墙已经足够(如果你是外网用户,毫无疑问已经足够!如果你是局域网用户,加个ARP防火墙,足矣) 有人说,系统墙防外不错,防内就不行了,其实是误解.只 ...
- zoj3591 Nim(Nim博弈)
ZOJ 3591 Nim(Nim博弈) 题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式). 方法是这样的,由于Nim博 ...
- HDU 4596 Yet another end of the world (数学,扩展欧几里德)
题意:给出n组x,y,z.判断是否存在一个id使得id%x1∈(y1,z1),id%x2∈(y2,z2). 析: 设 id/x1=a , id/x2=b ,则 id-a*x1=u; (1) id- ...