poj 282 Terrible Sets(堆栈)

it2022-05-05  128

Description

Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0. Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑0<=j<=i-1wj <= x <= ∑0<=j<=iwj} Again, define set S = {A| A = WH for some W , H ∈ R+ and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}. Your mission now. What is Max(S)? Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy. But for this one, believe me, it’s difficult. Input

The input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 and w1h1+w2h2+…+wnhn < 109. Output

Simply output Max(S) in a single line for each case. Sample Input

3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1 Sample Output

12 14

难到自闭。。。 有个博客对这个题的题目评价好:明明可以用形象的语言去描述,却非要用一堆排版粗劣的公式,还自以为很幽默。呵呵

#include<cstdio> #include<iostream> #include<algorithm> #include<stack> using namespace std; struct cur{ int x,y; }data; int main() { int n,i,j,p,m,k,a[500],sum; while(scanf("%d",&n)&&n!=-1){ int ans=0,tow,len=0; stack<cur>s;//栈底至栈顶高度递增 while(n--){ scanf("%d %d",&data.x,&data.y); if(data.y>len) s.push(data); else{ tow=0; while(!s.empty()&&s.top().y>data.y){ tow+=s.top().x; sum=tow*s.top().y; if(ans<sum) ans=sum; s.pop(); } data.x+=tow; s.push(data); } len=data.y; } tow=0; while(!s.empty()){ tow+=s.top().x; sum=tow*s.top().y; if(ans<sum) ans=sum; s.pop(); } printf("%d\n",ans); } return 0; }

我欲成仙,快乐西天。


最新回复(0)