[USACO17JAN] Cow Dance Show S 题解

· · 题解

题目传送门:link。

思路:二分答案,用 multiset 维护二分(去重),可以看成有 k 个位置,最终答案就是最大的位置上时间和。所以 k 次后插入时就把前面累计的时间加上即可,判断与 T_{max} 大小关系再二分即可。

如下:普通 set 只能得 30 分:

故用 multiset 去重。

code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

const int N=1e5+5,M=5005,inf=1e18;

int n,a[N],tmax;

int ck(int k){
    multiset<int> st;
    int tim=0;
    for(int i=1;i<=n;i++){
        if(i<=k){
            st.insert(a[i]);
        }
        else{
            int tmp=*st.begin();
            st.erase(st.begin());
            st.insert(tmp+a[i]);
        }
    }
    int maxx=-inf;
    for(auto v:st){
        maxx=max(maxx,v);
    }
    return (maxx<=tmax);
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>tmax;
    for(int i=1;i<=n;i++) cin>>a[i];
    int l=0,r=n+1;
    while(l+1!=r){
        int mid=(l+r)>>1;
//      cout<<mid<<": "<<ck(mid)<<endl;
        if(ck(mid)) r=mid;
        else l=mid; 
    }
    cout<<r;
    return 0;
}