luogu P1843 奶牛晒衣服

模拟T1,贪心+排序或者二分都行

贪心策略很好想,显然每次晒耗时最久的衣服最优,问题是要在每次晒完后都再次找到耗时最久的衣服,不能每次都sort,所以单调队列或者大根堆

二分也不难,直接二分时间,筛一遍衣服统计需要烘干的时间然后判断是否满足就行

模拟的时候敲了贪心+排序,所以这里放二分的代码(滑稽),个人感觉贪心更好写

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

int c[500005];
int n,a,b,i,j,k,y,l,r,m;
bool check(int x)
{
    k=0;
    for(i=1;i<=n;i++)
    {
        j=c[i]-a*m;
        if(j<=0) continue;
        y=j/b;
        if(j%b!=0) y++;
        k+=y;
        if(k>m) return 0;
    }
    if(k<=m) return 1;
    else return 0;
}

int main()
{
    cin>>n>>a>>b;
    for(i=1;i<=n;++i) scanf("%d",&c[i]);
    l=0,r=500010;
    while(l!=r)
    {
        m=(l+r)/2;
        if(check(m)) r=m;
        else l=m+1;
    }
    cout<<l;
    return 0;
}
原文地址:https://www.cnblogs.com/charlesss/p/10745837.html