poj 2082 Terrible Sets (前缀和,单调栈)
题意:这道题简直就是。。。教给大家怎么把一句话把简单的题让人出得看不懂。。。真的一点意思都没有。给出n个矩形的宽度和高度,这些矩形并排顺次排列在x轴上,问最大面积。
思路:单调栈。 之前的最大矩形面积的宽度都是1.。这次不是1.。做个宽度的前缀和就好。。。1A
/* ***********************************************
Author :111qqz
Created Time :2016年08月03日 星期三 18时54分40秒
File Name :code/poj/2082.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <stack>
8#include <set>
9#include <map>
10#include <string>
11#include <cmath>
12#include <cstdlib>
13#include <ctime>
14#define fst first
15#define sec second
16#define lson l,m,rt<<1
17#define rson m+1,r,rt<<1|1
18#define ms(a,x) memset(a,x,sizeof(a))
19typedef long long LL;
20#define pi pair < int ,int >
21#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=5E4+7;
7pi a[N];
8int l[N],r[N];
9int sum[N];
10int n;
11stack<int>stk;
12int main()
13{
14 #ifndef ONLINE_JUDGE
15 freopen("code/in.txt","r",stdin);
16 #endif
1 while (~scanf("%d",&n))
2 {
3 if (n==-1) break;
4 while (!stk.empty()) stk.pop();
5 sum[0] = 0 ;
6 for ( int i = 1 ; i <= n ; i++)
7 {
8 scanf("%d %d",&a[i].fst,&a[i].sec);
9 sum[i] = sum[i-1] + a[i].fst;
10 }
1 a[0].sec = -1;
2 a[n+1].sec = -1;
3 stk.push(0);
4 int x;
5 for ( int i = 1 ; i <= n ; i++)
6 {
7 for (x=stk.top() ; a[x].sec>=a[i].sec ; x = stk.top()) stk.pop();
8 l[i] = x+1;
9 stk.push(i);
10 }
1 while (!stk.empty()) stk.pop();
2 stk.push(n+1);
3 for ( int i = n ; i >=1 ; i--)
4 {
5 for (x=stk.top() ; a[x].sec>=a[i].sec ; x =stk.top()) stk.pop();
6 r[i] = x-1;
7 stk.push(i);
8 }
9 int ans = 0 ;
10 for ( int i = 1 ; i <= n ; i++) ans = max(ans,(sum[r[i]]-sum[l[i]-1])*a[i].sec);
11 printf("%d\n",ans);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}