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. 本地jar添加到本地仓库 本地jar依赖无效问题

    最近工作发生了一个很奇怪的事情,我在本地写了一个项目,打包成jar,然后敲命令mvn install:install-file -DgroupId=com.yzwine -DartifactId=yz ...

  2. Vitis下载安装尝试

    Vitis下载安装记录 一.下载安装 文章目录 一.下载安装 提示:以下是本篇文章正文内容,下面案例可供参考 一.下载安装 首先本次下载主要使用的是linux系统,所以我们先看一下Vitis支持的li ...

  3. LocalDateTime、OffsetDateTime、ZonedDateTime互转,这一篇绝对喂饱你

    前言 你好,我是A哥(YourBatman). 在JSR 310日期时间体系了,一共有三个API可用于表示日期时间: LocalDateTime:本地日期时间 OffsetDateTime:带偏移量的 ...

  4. 原生ajax分享

    最近被大佬问了一个很有趣的问题,你还能手打出一个ajax吗?,我当时的想法是有现成的为什么要自己打,后来我反思了一下(只有靠自己才是强者),在这里给大家分享一个我自己打的ajax,也是自己的一个知识点 ...

  5. proxmox ve系统绑定上联外网出口bond双网卡

    背景描述:一个客户搭建proxmox ve系统,要求上联出口双网卡绑定bond, proxmox ve下载地址:超链接 记录日期:2020/5/9 前期准备:服务器接好2个网卡 交换机:H3C 1.p ...

  6. 显示HDFS中指定的文件读写权限、大小、创建时间、路径等信息。

    1 import org.apache.hadoop.fs.*; 2 import java.text.SimpleDateFormat; 3 public class D_ReadFileStatu ...

  7. git的使用学习笔记--项目版本操作

    一.使用场景 版本回退:上线失败--需要回退到上个版本 二.操作 先编辑  vim text.txt git status git add .       这个命令能看到所有的增加操作 git com ...

  8. Git恢复之前版本的两种方法reset、revert

    实战 回退 1.删除之前的提交 git reset --hard id 推送到远程 git push -f [git log中确实删除了,但是拿到可以恢复] 2.不删除之前的提交 git revert ...

  9. C++ Primer Plus读书笔记(六)分支语句和逻辑运算符

    1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...

  10. 类型检查 Type Checking 一些编程语言并不安全 类型化语言的优点 定型环境 (符号表) 断言的种类

    Compiler http://staff.ustc.edu.cn/~bjhua/courses/compiler/2014/ http://staff.ustc.edu.cn/~bjhua/cour ...