1 solutions

  • 0
    @ 2024-2-27 13:13:19

    贪心策略:把最高的牛叠起来 AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    int arr[100100];
    bool cmp(int a,int b){
        return a>b;
    }
    int main(){
        int n,b,sum=0,ans=0;
        cin>>n>>b;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n,cmp);
        for(int i=0;i<n;i++){
            if(sum>=b){
                cout<<ans;
                return 0;
            }
            sum+=arr[i];
            ans++;
        }
        return 0;
    }
    
    • 1

    Information

    ID
    713
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    12
    Accepted
    10
    Uploaded By