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. Qt Undo Framework

    Qt undo/redo 框架 基于Command设计模式 支持命令压缩和命令合成 提供了与工具包其他部分融合很好的widgets和actions 术语(Terminology) Command - ...

  2. 解决PHP无法监听9000端口问题/502错误解决办法

    问题背景 配置nginx+php服务的时候,发现网站能打开html,打开php文件就显示502,一般这个是php没启动啊啥的导致不能正常解析php文件. 原因分析 因为nginx解析php文件是交给f ...

  3. Qt 自动化测试Test cutedriver

    示例 https://github.com/nomovok-opensource/cutedriver-examples CuteDriver examples This repository con ...

  4. How to Gracefully Close Channels

    小结: 1. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership ...

  5. 【网络安全】IOC概念浅析

    OpenIOC(Open Indicator of Compromise,开放威胁指标) MANDIANT 公司发布的情报共享规范,是开源.灵活的框架.OpenIOC是一个记录.定义以及共享威胁情报的 ...

  6. 分布式缓存 — Docker

    Docker 是一个开源项目,它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护. Doc ...

  7. 浅谈JavaScript异步编程

    单线程模式 我们知道JS的执行环境是单线程的,是因为JS语言最早是运行在浏览器端的语言,目的是为了实现页面上的动态交互.实现动态交互的核心就是DOM操作,因此决定了JS必须是单线程模式工作.我们来假设 ...

  8. Java——数据类型

    数据类型分类 基本数据类型: 数值型: 整数类型(byte,short,int,long): 浮点类型(float,double): 字符型(char): 布尔值(boolean): 引用数据类型: ...

  9. Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像)

    Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像) 一.geoserver+openlayers+mysql主要设计思想 1.1 Geo ...

  10. JavaWeb——EL及JSTL学习总结

    什么是EL表达式 为什么需要EL EL的主要作用 EL的语法 EL的开发步骤 EL实例练习 EL中的运算符 EL表达式显示内容的特点 EL的特点 EL隐式对象 EL隐式对象介绍 隐式对象实例练习 什么 ...