CSES – Ferris Wheel

貪心、資料結構

#include<bits/stdc++.h>
#define int long long
#define double long double
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
int n,x;
vector<int>a;
signed main(){
    fastio;
    cin>>n>>x;
    multiset<int,greater<int>> st;
    for(int i=0;i<n;i++){
        int x; cin>>x;
        st.insert(x);
    }
    int ans=1, cur=x, cnt=0;
    while(st.size()){
        auto it = st.lower_bound(cur);
        if(it == st.end() || cnt == 2){
            cur = x;
            ans++;
            cnt = 0;
        }
        else{
            int now = *it;
            st.erase(it);
            cur -= now;
            cnt++;
        }
    }
    cout<<ans<<"\n";
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *