2017.8.14--------单调队列

       今天感觉一直在研究单调队列,有的题一直WA,我也很无奈,以至于,气得不行了。

好不容易理解了,做出来了,能不能一遍过。。。心寒。

下面来一道WA的题,至今不知哪错了。。。提交了额~14次

 

 

Largest Rectangle in a Histogram

 

 

 

 

 

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 integer <i>n</i>, denoting the number of rectangles it is composed of. You may assume that <i>1<=n<=100000</i>. Then follow <i>n</i> integers <i>h<sub>1</sub>,...,h<sub>n</sub></i>, where <i>0<=h<sub>i</sub><=1000000000</i>. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is <i>1</i>. 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 3 4 1000 1000 1000 1000 0


 

Sample Output
  
  
8 4000

 

题意就是找最大的矩形面积

 

#include<iostream>
#include<stdio.h>
#define M 10000001
using namespace std;
long long h[M],mq[M],left1[M],right1[M];
long long t;


void Left()//找左边比他小的
{
    mq[0] = 0;//此作用就是做好位置标志
    long long front = 0, rear = 1;

    int i;
    for (i = 1; i <= t; i++)
    {
        while (front < rear && h[i] <= h[mq[rear-1]])
        {
            rear--;
        }
        left1[i] = i - mq[rear-1] - 1;//进行计算
        mq[rear++] = i;
    }
}

 

 

void Right()//右边
{
    mq[0] = t + 1;
    long long front = 0, rear = 1;

    int i;
    for (i = t; i >= 1; i--)//从最后一个开始
    {
        while (front < rear && h[i] <= h[mq[rear-1]])
        {
            rear--;
        }
        right1[i] = mq[rear-1] - i - 1;//注意顺序
        mq[rear++] = i;
    }
}
void jisuan()
{
    long long max1=-1;
    for(int i=1;i<=t;i++)
    {
        if((left1[i]+right1[i]+1)*h[i]>max1)
        max1=(left1[i]+right1[i]+1)*h[i];//计算最大面积
        //cout<<max1<<")))"<<endl;
    }
    cout<<max1<<endl;
}
int main()
{

    while(cin>>t&&t!=0)
    {
    for(int i=1;i<=t;i++)
    scanf("%d",&h[i]);
    Left();
    Right();
    jisuan();
    }
    return 0;
}

感觉很完美。。。。。这道题很基础,完全就是单调队列的灵活运用,最基础的几条。。。

WA就WA吧。。。。。。真的不知道哪不合适。。。。。~

 

 

第二道题:::Drying

就是洗衣服。。。奈何我看不懂题呀!!!!!

然后机洗,自然蒸发。

问最小的时间能是多少。。

假设mid分钟内洗完,带水量小于mid的自然不用机洗,大于mid的每件假设用机洗x分钟,则自然蒸干(mid-x)分钟,所有机洗的时间加起来不能超过mid

并且a[i]<=x*k+mid-x,

下面光荣附上我WA的代码!!!!!~~~~~

我也不知道哪里错了。。。。WAWAWAWWWAAAA

 

 

#include<iostream>
#include<cmath>
#define M 100001
using namespace std;
int a[M];
int main()
{
    long long t;
    while(cin>>t)
    {
        long long high=0;
        long long low=1;
        long long mid;
        long long k;

        for(int i=0;i<t;i++)
        {
            cin>>a[i];
            if(a[i]>high)
            high=a[i];
        }
        cin>>k;
        if(k==1)
        {
            cout<<high<<endl;
            continue;
        }


        int ans;
        while(low<=high)
        {
            long long  sum=0;
             mid=(high+low)/2;
            for(int i=0;i<t;i++)
            {
                if(a[i]>mid)
                {

                    if(((a[i]-mid)%(k-1))==0)
                    sum=sum+(a[i]-mid)/(k-1);
                    else
                    sum=sum+(a[i]-mid)/(k-1)+1;
                }
            }
            if(sum>mid)//mid取小了
            low=mid+1;
            else
            {
                ans=mid;
                high=mid-1;
            }


        }
        cout<<mid<<endl;
    }
        return 0;

}

好慢啊,今天收获就是看懂了队列,终于懂了,不开心的就是做一道题就WA,搞得很low的样子。。。灰头土脸的。。

我的理解好慢,但是懂了就很激动。

啊。时间太快了。明天又该新专题了。。。我这专题还没弄踏实呢。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值