1 条题解

  • 0
    @ 2026-9-30 12:59:26

    C++ :

    
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    struct Shop { int p, c; };
    static Shop s[1005];
    bool cmp(const Shop &x, const Shop &y){ return x.p < y.p; }
    /* 每行是「单价 存货量」,最小花费 ⇒ 按单价升序买,买光一家再下一家。 */
    int main(){
        int m, n;
        if(scanf("%d %d", &m, &n) != 2) return 0;
        for(int i = 0; i < n; i++) scanf("%d %d", &s[i].p, &s[i].c);
        sort(s, s + n, cmp);
        long long need = m, cost = 0;
        for(int i = 0; i < n && need > 0; i++){
            long long take = s[i].c < need ? s[i].c : need;
            cost += take * s[i].p;
            need -= take;
        }
        printf("%lld\n", cost);
        return 0;
    }
    
    
    • 1

    信息

    ID
    2857
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者