好久没更题解了(改题困难的我)
WYT有一把巨大的刷子,刷子的宽度为M米,现在WYT要使用这把大刷子去粉刷有N列的栅栏(每列宽度都为1米;每列的高度单位也为米,由输入数据给出)。
使用刷子的规则是:
WYT请你回答两个问题:
共两行:
第一行两个整数N和M。
第二行共N个整数,表示N列栅栏的高度
一行,两个整数,分别为最少剩余的单位面积数量和最少刷的次数。
5 3
5 3 4 4 5
3
2
样例对应解释
高度分别为 5 3 4 4 5 如上:
黄色的方块表示共有3个单位面积没刷上
绿色的框和红色的框表示一共刷了两次。
30%的数据:N<=10^3
50%的数据:N<=10^5
100%的数据:1<=N<=10^6, 1<=M<=10^6,N>=M, 每列栅栏的高度<=10^6.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
long long v[maxn],mh[maxn],sum,r[maxn],l[maxn];
long long n,m;
bool flag[maxn];
inline int read(){
int s=0;
char ch=getchar();
while(ch<‘0‘||ch>‘9‘)ch=getchar();
while(ch>=‘0‘&& ch<=‘9‘)s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s;
}
long long sta[maxn],top,h[maxn];
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++){
h[i]=read();
sum+=h[i];
}
for(int i=1;i<=n+1;i++){
while(top && h[i]<h[sta[top]]){
r[sta[top]]=i-sta[top];
top--;
}
sta[++top]=i;
}
for(int i=n;i>=0;i--){
while(top && h[i]<h[sta[top]]){
l[sta[top]]=sta[top]-i;
top--;
}
sta[++top]=i;
}
for(int i=1;i<=n;i++){
if(l[i]+r[i]-1>=m)flag[i]=true;
}
for(int i=1;i<=n;i++){
if(flag[i])mh[i]=h[i];
else{
mh[i]=mh[i-1];
}
}
for(int i=n;i>0;i--){
if(!flag[i])mh[i]=max(mh[i],mh[i+1]);
sum-=mh[i];
}
cout<<sum<<endl;
long long k=2,ans=0;
while(k<=n+1){
int cnt=1;
while(k<=n+1 && mh[k]==mh[k-1]){
++cnt;
++k;
}
ans+=cnt/m;
if(cnt%m)++ans;
++k;
}
cout<<ans<<endl;
}
原文:https://www.cnblogs.com/soda-ma/p/13226798.html