标签:input src img eve character contains follow tar 依次
InputThe 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.OutputFor 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 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
我先提出两种做法,一种是dp统计向左向右扩展的最大距离
这个之前有个合唱队的
N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合唱队形。
合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1, 2, …, K,他们的身高分别为T1, T2, …, TK,则他们的身高满足T1 < T2 < … < Ti , Ti > Ti+1 > … > TK (1 <= i <= K)。
你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形。
Input输入的第一行是一个整数N(2 <= N <= 100),表示同学的总数。第一行有n个整数,用空格分隔,第i个整数Ti(130 <= Ti <= 230)是第i位同学的身高(厘米)。Output输出包括一行,这一行只包含一个整数,就是最少需要几位同学出列。Sample Input
8 186 186 150 200 160 130 197 220
Sample Output
4
#include<stdio.h> int main() { int a[105],l[105],r[105],n; scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); for(int i=1; i<=n; i++) { l[i]=r[n-i+1]=1; for(int j=1; j<i; j++) { if(a[i]>a[j]&&l[i]<=l[j]) l[i]=l[j]+1; if(a[n-i+1]>a[n-j+1]&&r[n-i+1]<=r[n-j+1]) r[n-i+1]=r[n-j+1]+1; } } int ans=0; for(int i=1; i<=n; i++) if(l[i]+r[i]>ans)ans=l[i]+r[i]; printf("%d\n",n-ans+1); return 0; }
这个直接这样也可以过的,无非就是乘了个高度和个数
单调栈水过
#include<stdio.h> #include<string.h> typedef __int64 LL; const int maxn = 100005; LL a[maxn],sta[maxn],left[maxn]; int main() { int N; while (~scanf("%d",&N)) { if(!N)break; LL res = 0,tmp; memset(sta,0,sizeof(sta)); memset(left,0,sizeof(left)); for (int i = 1; i <= N; i++) scanf("%I64d",&a[i]); a[++N] = -1; int top = 0; for (int i = 1; i <= N; i++) { if (!top||a[i]>a[sta[top-1]]) { sta[top++] = i; left[i] = i; continue; } if (a[i] == a[sta[top-1]]) continue; while (top > 0 && a[i] < a[sta[top-1]]) { top--; tmp = a[sta[top]]*((i-1)- (left[sta[top]]-1)); res = res<tmp?tmp:res; } tmp = sta[top]; sta[top++] = i; left[i] = left[tmp]; } printf("%I64d\n",res); } return 0; }
Largest Rectangle in a Histogram
标签:input src img eve character contains follow tar 依次
原文地址:http://www.cnblogs.com/BobHuang/p/7363876.html