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. 【高并发】ReadWriteLock怎么和缓存扯上关系了?!

    写在前面 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在并发场景中, ...

  2. 24v转3.3v稳压芯片,高效率DC-DC变换器3A输出电流

    PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...

  3. 在EXCEL中如何同时冻结行与列?

    鼠标所在的单元格的位置 ,决定了你冻结的行和列.如: 冻结第一行与第一列, 只需要将鼠标置于单元格在第二列,第二行. 点击冻结

  4. IE浏览器兼容问题总结

    IE浏览器兼容问题总结 引自掘金:https://juejin.cn/post/6844903825854185480 一.标准盒模型和怪异盒模型 浏览器的盒子模型分为两类: 标准的W3C盒子模型. ...

  5. 请谨慎使用 avaliable 方法来申请缓冲区

    问题 今天开始尝试用 Java 写 http 服务器,开局就遇到 Bug. 我先写了一个多线程的.BIO 的 http 服务器,其中接收请求的部分,会将请求的第一行打印出来. 下面是浏览器发出的请求和 ...

  6. 不错的网站压力测试工具webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.前期准备:yum install ...

  7. 阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网IP资源,可以绑定到阿里云专有网络VPC类型的ECS、NAT网关、私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS、NAT网关、SLB的解耦,满足灵活管理的要求。阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性络VPC类型的E

    阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网关.私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS.NAT网关.SLB的解耦,满足灵活管理的要求.

  8. GDB 简单学习

    一般来说,GDB主要帮忙你完成下面四个方面的功能:       1.启动你的程序,可以按照你的自定义的要求随心所欲的运行程序.     2.可让被调试的程序在你所指定的调置的断点处停住.(断点可以是条 ...

  9. PAT甲级1056Mice and Rice

    目录 题目介绍 题解 解题思路 代码 参考链接 题目介绍 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/9948054 ...

  10. 基于Koa2框架的项目搭建及实战开发

    Koa是基于 Node.js 平台的下一代 web 开发框架,由express原班人马打造,致力于成为一个更小.更富有表现力.更健壮的 Web 框架.使用 koa 编写 web 应用,通过组合不同的 ...