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.

Examples
Input
5
1 2 4 5 3
Output
7

(个人觉得这题的思路和5c很像,因为都是我想不出来的思路- -

题意:(题意是不可能读懂的
    让我队友帮我翻译了下,大致知道是给一个环,环上有n个点,如果每两个点之间没有比这两个点大的点,则这两个点能连一条线,问最多能连几条线。

解题思路:
    假设对于i,我们已经计算出了left[i], right[i], same[i],其中left[i]表示i左侧比第一个比i高的位置,right[i]表示i右侧第一个比i高的位置,same[i]表示从i到right[i]的左开右闭区间内高度等于i的山的数目。

     简而言之,left和right是位置,而same是数目。

     那么对于一座山i而言,它可以和left[i] 和 right[i]都构成能互相看见的pair,并且和i到right[i]之间所有高度等于i的山构成能互相看见的pair。

     所以问题就是计算left数组、right数组和same数组。(搜题解的

 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 using namespace std;
5 #define Max(a,b) a>b?a:b
6 const int maxn=1000005;
7 int a[maxn],b[maxn],lef[maxn],rig[maxn],c[maxn]={0};
8 int main()
9 {
10 int n;
11 scanf("%d",&n);
12 int ma=-1,mid=0;
13 for(int i=0;i<n;i++)
14 {
15 scanf("%d",&a[i]);
16 if(a[i]>ma)
17 {
18 ma=a[i];
19 mid=i;
20 }
21 }
22 mid--;
23 for(int j=1;j<=n;j++)
24 b[j]=a[(mid+j)%n]; //处理数组
25 lef[1]=1;
26 for(int i=2;i<=n;i++)
27 {
28 lef[i]=i-1;
29 while(lef[i]>1&&b[lef[i]]<=b[i])
30 lef[i]=lef[lef[i]];//往左递归
31 }
32 for(int i=n;i>=1;i--)
33 {
34 rig[i]=i+1;
35 while(rig[i]<=n&&b[rig[i]]<b[i])
36 rig[i]=rig[rig[i]];//往右递归
37 if(rig[i]<=n&&b[rig[i]]==b[i])
38 {
39 c[i]=c[rig[i]]+1;
40 rig[i]=rig[rig[i]];
41 }
42 }
43 long long ans=0;
44 for(int i=2;i<=n;i++)
45 {
46 ans+=c[i]+2;
47 if(lef[i]==1&&rig[i]==n+1)
48 ans--;
49 }
50 printf("%lld\n",ans);
51 }

参考博客:http://blog.csdn.net/aholic/article/details/28155751

     http://blog.csdn.net/tree__water/article/details/52090499    

codeforces 5E(非原创)的更多相关文章

  1. codeforces 6E (非原创)

    E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...

  2. Linux下high CPU分析心得【非原创】

    非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...

  3. CSS样式命名整理(非原创)

    非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...

  4. 非原创。使用ajax加载控件

    非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...

  5. Java 表达式解析(非原创)

    因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...

  6. Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)

    Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...

  7. 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)

    我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果)  这是原博客 http://news.cnblogs.com/n/ ...

  8. tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)

    phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...

  9. Vue 仿QQ左滑删除功能(非原创)

    非原创,摘选来源:http://www.jb51.net/article/136221.htm. 废话不多说,相当实用,先记录. Html代码: <div class="contain ...

  10. 老男孩Django笔记(非原创)

    .WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...

随机推荐

  1. windows下的:开始→运行→命令

    开始→运行→命令 集锦                          winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...

  2. webapi Swagger 配置 services.BuildServiceProvider() 报警 ASP0000 问题处理

    问题起源 网上的常见配置 Swagger 配置 在Startup类的 ConfigureServices 使用 services.BuildServiceProvider() ,其中有段代码如下: v ...

  3. xtrabakcup基本用法 安装、全量备份恢复、增量备份恢复

    xtrabackup备份原理以及工作流程 备份流程日志分析:1.##读取mysql配置文件2.## 扫描innodb日志lsn并复制inndodb系统表空间3.## 缓冲写出到数据文件并锁表4.## ...

  4. python的零碎知识

    1.Python代码操作git 安装 pip3 install gitpython 操作git import os from git.repo import Repo # gitpython def ...

  5. 精通MySQL之架构篇

    老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...

  6. c#使用谷歌身份验证GoogleAuthenticator

    此功能相当于给系统加了个令牌,只有输入对的一组数字才可以验证成功.类似于QQ令牌一样. 一丶创建最核心的一个类GoogleAuthenticator 此类包含了生成密钥,验证,将绑定密钥转为二维码. ...

  7. 解决window10 和 ubuntu 双系统安装没有启动选项问题

    win10 和Ubuntu 双系统安装在网上已经有很多例子了,这里就不在赘述了. 今天新买的笔记本,想安装双系统.正常安装完ubuntu 重启后没有选项. 解决方法一 下载和解压以后,按照以下的步骤安 ...

  8. In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego Ongaro and John Ousterhout.

    In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego ...

  9. 为什么Go自带的日志默认输出到os.Stderr?

    为什么Go自带的日志默认输出到os.Stderr? - 知乎 https://www.zhihu.com/question/67629357 Note that the Go runtime writ ...

  10. JetBrains(IEDA、CLion、Pycharm) 学生获得免费使用资格

    JetBrains针对学生推出了免费使用资格,但是很多同学却不知道或者说不知道怎样获得免费资格,只能千辛万苦的去寻找破解密钥,但现在JetBrains针对盗版的封锁越来越严密,很多密钥过一段时间就会失 ...