2017.3.13 E

来源:互联网 发布:安卓优化软件排行榜 编辑:程序博客网 时间:2024/04/30 20:11

对于

原来只是单纯地循环所以超时;


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 integern, denoting the number of rectangles it is composed of. You may assume that1<=n<=100000. Then follow n integers h1,...,hn, where0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is1. 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

还是太幼稚了  10000000的10000单纯地循环怎么可以搞定

#include <iostream>#include<algorithm>#include<map>using namespace std;const int maxn=100005;const int maxh=1000000005;int h[maxn],od[maxn];long long s;long long maxs;int n;long long judge(int t){    long long max2=0;    long long  ss1=0;    for(int i=t;i<n;i++)    {       if(h[i]>=h[t])        ss1+=h[t];       else        break;    }    for(int i=t-1;i>=0;i--)    {         if(h[i]>=h[t])           ss1+=h[t];         else          break;    }    return ss1;}int main(){    while(cin>>n&&n)    {        int maxs=0;        for(int i=0;i<n;i++)         {              cin>>h[i];              od[i]=h[i];         }        for(int i=0;i<n;i++)        {             if(h[i]*n<=maxs)               continue;             s=judge(i);            if(s>maxs)                maxs=s;        }        cout << maxs << endl;    }    return 0;}

大神的思维就是巧,用了一个栈,干干净净的就解决了,用一个栈,存储当前高度以及当前可组合数目,而且很好的解决了前高后低情况的问题

#include <iostream>#include<stack>using namespace std;stack<pair<int,int> > s;int main(){    int n;    while(cin>>n&&n)    {        long long ans=0,num=0;        for(int i=0;i<n;i++)        {            long long x;            cin>>x;            num=0;//开始无            while(!s.empty()&&s.top().first>=x)//如果矮或等高就进入开始            {                num+=s.top().second;                ans=max(ans,num*s.top().first);                s.pop();            }            s.push(make_pair(x,num+1));        }        num=0;//一定不能忘记清零        //最后计算      while(!s.empty())        {            num+=s.top().second;            ans=max(ans,num*s.top().first);            s.pop();        }        cout << ans << endl;    }    return 0;}


0 0