codeforces 5E(非原创)
4 seconds
256 megabytes
standard input
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.
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.
Print the required amount of pairs.
5
1 2 4 5 3
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(非原创)的更多相关文章
- codeforces 6E (非原创)
E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...
- Linux下high CPU分析心得【非原创】
非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...
- CSS样式命名整理(非原创)
非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...
- 非原创。使用ajax加载控件
非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...
- Java 表达式解析(非原创)
因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...
- Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)
Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...
- 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)
我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果) 这是原博客 http://news.cnblogs.com/n/ ...
- tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)
phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...
- Vue 仿QQ左滑删除功能(非原创)
非原创,摘选来源:http://www.jb51.net/article/136221.htm. 废话不多说,相当实用,先记录. Html代码: <div class="contain ...
- 老男孩Django笔记(非原创)
.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
随机推荐
- [Usaco 2012 Feb]Nearby Cows
题目描述 FJ发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃.FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地, ...
- 解决maven中某些依赖无法下载,手动安装Maven依赖
<!--先下载jar包,然后在仓库中手动安装,下面是遇到的两个例子--> <!--第一个--> mvn install:install-file -Dfile=D:\kaptc ...
- (03)-Python3之--元组(tuple)操作
1.定义 元组的关键字:tuple 元组以()括起来,数据之间用 , 隔开.元组当中的数据,可以是任意类型.数值是可以重复的. 元组元素是 不可变的,顺序是 有序的. 例如: b = ("萝 ...
- 8.3 Customizing Git - Git Hooks 钩子 自动拉取 自动部署 提交工作流钩子,电子邮件工作流钩子和其他钩子
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks https://github.com/git/git/blob/master/temp ...
- 洛谷P3501
Description 对于一个 \(0/1\) 串,如果取反后再将整个串反过来和原串一样,就称作「反对称」字符串 给出一个长度为 \(n\) 的 \(0/1\) 串,求它有多少个反对称子串 Solu ...
- tricks - 思维
编辑 目录 tricks 系列 随机的性质 bitmask 建图 最基本的 黑白染色 Kruskal重构树 并查集维护值域 带根号的数三元环 根号分治 调和级数哈希 多属性哈希 时光倒流 时光反复横跳 ...
- 你真的知道为什么要使用void(0)代替undefined吗?
我们平时用到的\(\color{#FF3030}{undefined}\)只是\(\color{#FF3030}{window}\)对象下的一个属性. Object.getOwnPropertyDes ...
- Java中String对象创建机制详解()
一String 使用 private final char value来实现字符串存储 二Java中String的创建方法四种 三在深入了解String创建机制之前要先了解一个重要概念常量池Const ...
- Scala:case class
Scala:case class 1.Scala中class.object.case class.case object区别 1.1 class 和 object 关系 1.2 case class ...
- Java 高并发 解决方案
1.HTML静态化 2.图片服务器分离 3.数据库集群和库表散列 4.缓存 5.镜像 6.负载均衡 1)硬件四层交换 2)软件四层交换 一:高并发高负载类网站关注点之数据库 二:高并发高负载网站的系统 ...