Codeforces Beta Round #5 E. Bindian Signalizing 并查集
2015-06-23 12:30
316 查看
E. Bindian Signalizing
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/problemset/problem/5/EDescription
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.
[b]Input[/b]
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.
[b]Output[/b]
Print the required amount of pairs.
[b]Sample Input[/b]
5
1 2 4 5 3
[b]Sample Output[/b]
7
HINT
[b]题意[/b]给你一个环的山,如果两个山之间没有比这座山更高的山的话,那么这两座山是可以互相看见的,然后问你最后有多少座山可以互相看见
[b]题解:[/b]
首先我们把环变成一个链,然后用一个并查集的思想,处理每一座山左边第一个比这个山高的山是哪个,右边第一个比这个山高的是哪个,这个区域内和这个山高度一样的山有多少
然后跑一发并查集就好了
[b]代码[/b]
//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[20]; //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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } 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(""); } //************************************************************************************** int a[maxn]; int b[maxn]; int l[maxn]; int r[maxn]; int k[maxn]; int main() { int n=read(); int p=-1; for(int i=0;i<n;i++) { a[i]=read(); if(p==-1) p=i; else if(a[i]>a[p]) { p=i; } } for(int i=0;i<n;i++) b[i]=a[(i+p+n)%n]; b =a[p]; for(int i=1;i<n;i++) { l[i]=i-1; while(l[i]>0&&b[i]>=b[l[i]]) l[i]=l[l[i]]; } k =0; for(int i=n-1;i>=0;i--) { r[i]=i+1; 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]]+1; r[i]=r[r[i]]; } } ll ans=0; for(int i=1;i<n;i++) { ans+=k[i]; ans+=1; if(l[i]!=0||r[i]!=n) ans++; } cout<<ans<<endl; }
相关文章推荐
- 火狐下<a>标签里嵌套的<select>不能选的bug
- Swift基础教程
- 关于TCP的几点疑惑???
- 修改Hibernate实体类字段int为Integer产生java.lang.NoSuchMethodError
- 如何对所发现的缺陷进行严密的等级划分
- 设计模式之适配器模式
- 以此勉励
- solr/solrj之原子更新
- 连接队列相关1
- Advanced.Aircraft.Analysis.v2.5.1.53 1CD(飞机设计性能分析软体)
- Android小技巧:实现捕获应用的运行时异常
- 停靠技术 Dock
- Android小技巧:实现捕获应用的运行时异常 分类: Android其他 2015-06-23 12:26 12人阅读 评论(0) 收藏
- Flexisign Pro v10.5.1 Build 1806-ISO 1DVD(电脑切割系统)
- 这篇文章证实了我的猜测:即:在TCP中,存在 :全连接队列,半连接队列,等队列
- sublime text3安装插件 emmet
- HTML5
- jQuery 实验教程
- Sokkia Mapsuite+ v3.0 build 304-ISO 1CD(勘探测绘软件)
- Python中线程编程之threading模块的使用详解