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> ...
随机推荐
- ORACLE TM锁
Oracle的TM锁类型 锁模式 锁描述 解释 SQL操作 0 none 1 NULL 空 Select 2 SS(Row-S) 行级共享锁,其他对象只能查询这些数据行 Select for upda ...
- <转>安卓软件测试的几个要点
1.界面 ① 文字错误.图片不显示或显示不正确.缺少输入项.按钮的大小和点击效果 ② 布局.图片和配色设计问题,测试人员很难进入 ③ 提示信息,提示信息语言准确简洁,有指导性.在应该提示的位置放入提示 ...
- 让pre标签自动换行示例代码
pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码.对于技术博客经 ...
- LeetCode Database: Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- Jquery 遍历表单 AJAX提交
function test(){ var arrayObj = new Array(); $("#contentTable tbody tr").each(function(){ ...
- cocos2dx 内存管理
转载自 ocos2dx 内存管理 - 小花原创博客 - 博客频道 - CSDN.NET http://blog.csdn.net/ring0hx/article/details/7946397 coc ...
- Azure Cloud中的Log4Net设置
这里的Cloud包含Worker Role和Web Role,Role是运行在云主机中的,这里的主机和VM有所不同,Windows Azure Role Architecture.我们并没有和本地服务 ...
- 【转】Java transient关键字
Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...
- HDU 2042 不容易系列之二 [补6.24] 分类: ACM 2015-06-26 20:40 9人阅读 评论(0) 收藏
不容易系列之二 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- C++11之使用或禁用对象的默认函数
[C++11之使用或禁用对象的默认函数] C++11 允许显式地表明采用或拒用编译器提供的内置函数.例如要求类型带有默认构造函数,可以用以下的语法: 另一方面,也可以禁止编译器自动产生某些函数.如下面 ...