1 solutions

  • 0
    @ 2024-6-5 13:45:09

    贪心(nlognnlog^n)

    解题思路

    先将所有物品按照价值从小到大枚举,然后从大到小枚举枚举每个物品,如果当前物品可以带走一个小的物品,就带走一个小的物品。

    #include<bits/stdc++.h>
    using namespace std;
    const int N=30010;
    int n,m;
    int w[N];
    int main()
    {
        cin>>m>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
        }
        sort(w+1,w+n+1); //从小到大排序 
        int res=0;
        for(int j=n,i=1;i<=j;j--) //每次取一个大的 
        {
            if(w[i]+w[j]<=m) //可以带一个小的 
            {
                 i++;   
            }
            res++;
        }   
        cout<<res;
        return 0;
    }
    
    • 1

    Information

    ID
    418
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    4
    Accepted
    3
    Uploaded By