codeforces 5E Bindian Signalizing
来源:互联网 发布:淘宝男装免费代理 编辑:程序博客网 时间:2024/06/05 03:43
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.
题目的意思是有n座围成一个圈的山,站在两个山头能互相看见当且仅当:两座山之间没有比他们更高的山。
给出n座山的高度,输出能相互看见的pair数。
首先将输入的数字顺序预处理一下,找到最高的山峰,将这个圈从该处切断成链。
最简单的就是暴力了,对于一座山,枚举的判断其他山能否和他互相看见,这样的复杂度是O(n^2),这题好像数据量略大,这样的复杂度是过不了的。
经过查看别人的解题报告,有这么一种dp的方法:
假设对于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数组。
可以考虑用动态规划的思想来做,具体的见代码吧,但是这样的一个求left数组的算法的复杂度不是很好分析,我感觉和kmp算法的复杂度有点类似,好难分析。。。。。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <stack>#include <map>#include <queue>#include <algorithm>using namespace std;int main() { int n; scanf("%d", &n); vector<int> height(n); for (int i = 0; i < n; i++) scanf("%d", &(height[i])); vector<int>::iterator highest = max_element(height.begin(), height.end()); //cut the circle, make a chain vector<int> num(highest, height.end()); num.reserve(n); copy(height.begin(), highest, num.end()); //left[i] is the nearest bigger number in the left of num[i] vector<int> left(n, -1); for (int i = 1; i < n; i++) { int p = i - 1; while (num[i] > num[p]) p = left[p]; left[i] = num[i] == num[p] ? left[p] : p; } //right[i] is the nearest bigger number in the right of num[i] //same[i] is the number of the number equal to num[i] in [i, right[i]] vector<int> right(n, n); vector<int> same(n, 0); for (int i = n - 2; i >= 0; i--) { int p = i + 1; while (p < n && num[i] > num[p]) p = right[p]; if (p >= n) { right[i] = p; continue; } right[i] = num[i] == num[p] ? right[p] : p; same[i] = num[i] == num[p] ? same[p] + 1 : 0; } long long ans = 0; for (int i = 0; i < n; i++) { if (left[i] >= 0) ans += 1; if (right[i] < n) ans += 1; if (right[i] >= n && left[i] > 0) ans += 1; ans += same[i]; } printf("%I64d\n", ans); return 0;}
- codeforces 5E Bindian Signalizing
- Codeforces 5E Bindian Signalizing
- Codeforces 5E. Bindian Signalizing
- codeforces 5E Bindian Signalizing
- Codeforces E.Bindian Signalizing
- Codeforces Beta Round #5 E. Bindian Signalizing
- Codeforces 5E Bindian Signalizing 题解
- codeforces 5.E. Bindian Signalizing
- CF 5 E. Bindian Signalizing
- 2017京东笔试编程题-保卫方案 | Codeforces Beta Round #5 E.Bindian Signalizing
- E. Bindian Signalizing (拆环成链)(好题)
- CF5E Bindian Signalizing
- Codeforces5E - Bindian Signalizing
- CodeFOrces 5E
- Codeforces 5E
- Codeforces Beta Round #5 E
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- matlab reshape使用
- jrtplib3.9.1 example2
- 阿里负责人揭秘面试潜规则
- JAVA获取路径问题
- eclipse在进行linux嵌入式开发时环境变量的设置问题
- codeforces 5E Bindian Signalizing
- 判断一个字符串是否能转化为数字方法
- bzoj1624 [Usaco2008 Open] Clear And Present Danger 寻宝之路
- 堆与栈的区别
- C++模板元编程 入门简介
- 几个复古的页面应用-------Day25
- Unique Paths
- easu ui 下拉框选择某项时打开对应的网页 onSelect() window.onen()
- CSS浏览器兼容问题集(一)