1 条题解

  • 0
    @ 2025-10-8 16:53:43
    #include<bits/stdc++.h>
    using namespace std;
    int p[300], b[300], len;
    int main()
    {
        int M, S, C; scanf("%d%d%d", &M, &S, &C);
        for(int i=1; i<=C; i++) scanf("%d", &p[i]);
        sort(p+1, p+C+1);
        len = 0;
        for(int i=2; i<=C; i++) if(p[i]-p[i-1]-1>0) b[++len] = p[i]-p[i-1]-1; //空格段 
        sort(b+1, b+len+1);
        int ans = p[C] - p[1] + 1, t = 1;
        if(M>1) for(int i=len; i>=1; i--) { ans -= b[i]; t++; if(t == M) break; } //反向思维:减去最长的空格段 
        printf("%d\n", ans);
        return 0;
    }
    
    • 1

    A27【贪心】[USACO1.3] 修理牛棚 Barn Repair

    信息

    ID
    825
    时间
    1000ms
    内存
    128MiB
    难度
    7
    标签
    递交数
    320
    已通过
    76
    上传者