今天感觉一直在研究单调队列,有的题一直WA,我也很无奈,以至于,气得不行了。
好不容易理解了,做出来了,能不能一遍过。。。心寒。
下面来一道WA的题,至今不知哪错了。。。提交了额~14次
Largest Rectangle in a Histogram
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.
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
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的样子。。。灰头土脸的。。
我的理解好慢,但是懂了就很激动。
啊。时间太快了。明天又该新专题了。。。我这专题还没弄踏实呢。