其他分享
首页 > 其他分享> > Problem D. Ice Cream Tower 题解(二分+贪心)

Problem D. Ice Cream Tower 题解(二分+贪心)

作者:互联网

题目链接

题目思路

二分答案

然后贪心check

这个贪心有点意思

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define debug printf("aaaaaaaaaaa\n");
const int maxn=3e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,k;
int tot=0;
ll a[maxn];
ll num[maxn];
bool check(int x){
    int pos=1;
    int lun=0;
    for(int i=1;i<=x;i++){
        num[i]=0;
    }
    for(int i=1;i<=n;i++){
        if(a[i]>=2*num[pos]){
            num[pos]=a[i];
            pos++;
            if(pos==x+1){
                lun++;
                pos=1;
            }
        }
    }
    return lun>=k;

}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int _; cin>>_;
    while(_--){
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        int l=1,r=n/k,ans=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        cout<<"Case #"<<++tot<<": "<<ans<<'\n';
    }
    return 0;
}

标签:int,题解,ll,pos,Ice,num,maxn,Problem,lun
来源: https://www.cnblogs.com/hunxuewangzi/p/15021610.html