动态规划D
来源:互联网 发布:商品sku数据库设计 编辑:程序博客网 时间:2024/05/16 11:26
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, …, hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000
Sample Output
84000
import java.util.ArrayList;import java.util.HashMap;import java.util.Scanner;/** * Created by 95112 on 10/23/2017. */public class LargestRectangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Long> answers = new ArrayList<>(); int n ; do { n = scanner.nextInt(); if (n == 0) break; HashMap<Integer,Integer> map = new HashMap<>(); int[] left = new int[n]; int[] right = new int[n]; int[] input = new int[n]; left[0] = 0; right[n-1] = n-1; for (int i = 0 ; i < n; i ++ ){ input[i] = scanner.nextInt(); } long sum = 0; for (int i = 1 ; i < n ; i++) { int j =i ; while (j > 0 && input[j-1] >= input[i] ) { j = left[j-1]; } left[i] = j; } for (int i = n-2; i >=0 ; i--) { int j = i ; while ( j < n - 1 && input[j+1] >= input[i]) { j = right[j+1]; } right[i] = j; } for (int i= 0 ; i< n; i++) { Long length = Long.valueOf((right[i] - left[i] +1 )); Long tmp = length * input[i]; if (tmp > sum){ sum = length * input[i]; } } answers.add(sum); }while (true); for (Long answer : answers) System.out.println(answer); }}
阅读全文
0 0
- 动态规划D
- [预处理优化][动态规划]D
- codeforces 229/D 动态规划
- 费波纳契数d动态规划实现
- Codeforces 611D 动态规划
- 动态规划—Problem D
- codeforces D. Palindrome pairs 动态规划
- codeforces #186 D 动态规划(DP)
- codeforces 4D Mysterious Present -- 动态规划
- Codeforces 474D Flowers 动态规划法
- Gym 100712D Alternating Strings 动态规划
- codeforces 484D Kindergarten 【动态规划】
- Codeforces 809D [平衡树][动态规划]
- 2D/1D动态规划的四边形优化
- 1D/1D动态规划优化初步 模型一
- codeforces 303 D. Ilya and Roads(动态规划)
- codeforce 359D 二分+ 动态规划(sparse table)
- Codeforces 448D(动态规划、类H数)
- hadoop 上传文件到HDFS报错
- 9.2 mnist_with_summaries tensorboard 可视化展示
- VueJS-组件通信
- three.js学习笔记(三)-camera.up属性探索
- beam整合flink
- 动态规划D
- 分页sql
- Quartz配置参考
- Spring框架AOP源码剖析
- 静态页面(动态页面)引入静态页面(动态页面)
- Linux基础命令
- 树种统计(25 分)
- centos 安装opencv3.3遇到的问题
- muduo网络库学习(三)定时器TimerQueue的设计