Codeforces Round #305 B题 思维+贪心
2015-05-29 21:21
183 查看
B. Mike and Feet
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.
A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.
Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.
Input
The first line of input contains integer n (1 ≤ n ≤ 2 × 10^5), the number of bears.
The second line contains n integers separated by space, a1, a2, …, an (1 ≤ ai ≤ 10^9), heights of bears.
Output
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
Sample test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output
6 4 4 3 3 2 2 1 1 1
然后贪心求解,对于位置site,它有两个属性,height和width,以height为第一优先排序
每次从排序后的集合取最优先的值,如果他的width大与当前取的次数,那么输出这个height,否则重复当前步骤
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.
A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.
Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.
Input
The first line of input contains integer n (1 ≤ n ≤ 2 × 10^5), the number of bears.
The second line contains n integers separated by space, a1, a2, …, an (1 ≤ ai ≤ 10^9), heights of bears.
Output
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
Sample test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output
6 4 4 3 3 2 2 1 1 1
题意:x从1到n,求长度x的这些子区间的最大值,每个区间的最大值定义为该区间的最小值
解法:
求出每个值x向左和右以x为最小值所能延伸的范围width然后贪心求解,对于位置site,它有两个属性,height和width,以height为第一优先排序
每次从排序后的集合取最优先的值,如果他的width大与当前取的次数,那么输出这个height,否则重复当前步骤
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cassert> using namespace std ; const int N = 2e5 + 11 ; struct Node { int h , w ; }; int tol , tor ; int height ; Node arr ; bool cmp(const Node& a , const Node& b) { if(a.h == b.h) return a.w > b.w ; return a.h > b.h ; } int main() { //freopen("data.in", "r" , stdin); int n ; while(scanf("%d" ,&n)==1) { for(int i = 1 ; i <= n; ++i) scanf("%d" ,&height[i]) ; tol[1] = 1 ; tor = n ; for(int i = 1 ; i <= n ; ++i) { int tmp = i ; while(tmp > 1 && height[tmp-1] >= height[i]) tmp = tol[tmp-1] ; tol[i] = tmp ; } for(int i = n-1 ; i >= 1 ; --i) { int tmp = i ; while(tmp < n && height[tmp+1] >= height[i]) tmp = tor[tmp+1] ; tor[i] = tmp ; } for(int i = 1 ; i <= n ; ++i) { arr[i-1].w = (tor[i]-tol[i]+1) ; arr[i-1].h = height[i] ; } sort(arr , arr+n , cmp) ; int last = 0 ; for(int i = 0 ; i < n ; ++i) { if(arr[i].w > last) { printf("%d" , arr[i].h) ; ++last ; if(last == n) {printf("\n") ;break;} else printf(" ") ; --i ; } } } }
相关文章推荐
- 灵悟礼品网上专卖店——客户端的登录功能开发
- AOP之代理模式(一)
- itop4412的linux环境搭建
- ios数据保存
- 点类派生直线类
- 日志分析(七)日志处理
- 黑马程序员 C语言:指针的概念
- CLang之简单介绍
- CSS--使用Animate.css制作动画效果
- 杭电acm the area
- 测试计划
- 使用CNN(convolutional neural nets)检测脸部关键点教程(二):浅层网络训练和测试
- getAttribute()和getParameter()区别
- 《Entity Framework 6 Recipes》中文翻译系列 (32) ------ 第六章 继承与建模高级应用之TPH与TPT (1)
- xml学习
- JAVA快速排序算法
- 软考总计之白盒测试中的覆盖
- 大二学生一枚 写下自己学51和PCB的感想 与各位刚开始学习的盆友共勉 一起加油
- 从底层理解Python的执行
- Andriod SDK出现Failed to parse the output of 'adb version'问题