首先最暴力的方法就是2个循环,枚举每一个矩形,往左边找第一个高度小于当前的矩形,然后求面积,即(wi-wj)*hi,在i和j矩形之间的矩形的高度都是大于i和j的,因此可以用一个单调递增的栈维护,对于矩形i,弹出栈尾小于等于i高度的矩形,并且计算面积,最后栈中都是高度单调递增的矩形。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
class node {
int w, h;
node(int w, int h) {
this.w = w;
this.h = h;
}
}
node[] a;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n == -1) {
break;
}
a = new node[n+1];
for (int i = 1; i <= n; i++) {
int w = scanner.nextInt(), h = scanner.nextInt();
a[i] = new node(w, h);
}
ArrayList<node> st = new ArrayList();
st.add(new node(0, 0));
int ans = 0, width = 0;
for(int i = 1; i <= n; i++) {
while (st.size() > 1 && st.get(st.size()-1).h >= a[i].h) {
int w = width-st.get(st.size()-2).w;
int h = st.get(st.size()-1).h;
int tmp = w*h;
ans = Math.max(ans, tmp);
st.remove(st.size()-1);
}
width += a[i].w;
st.add(new node(width, a[i].h));
}
while (st.size() > 1) {
int w = width-st.get(st.size()-2).w;
int h = st.get(st.size()-1).h;
int tmp = w*h;
ans = Math.max(ans, tmp);
st.remove(st.size()-1);
}
System.out.println(ans);
}
}
}