1 条题解

  • 2
    @ 2023-11-28 20:04:21

    找到了,找了半天找到了我洛谷上的代码,其实就是个贪心,贪心策略是:让放的高的奶牛的体重越小并且力量越小,至于证明嘛:

    以下是证明, 来自洛谷的神犇,因为蒟蒻懒得想太多(读者:作者真懒,这都懒得想) 假设x, y为相邻的奶牛,

    且 Wx + Sx < Wy + Sy;

    那么 Wx - Sy < Wy-Sx;

    那么显然是右边的情况更优,窝们就应该把x放在上面,y在下面。

    下面是ACAC CodeCode

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll n, ans = -10000000, x;
    struct cow
    {
        ll w, s, cnt;
    }a[50005];
    bool cmp(cow x, cow y)
    {
        return x.cnt < y.cnt;
    }
    signed main()
    {
        // freopen("P1842.in", "r", stdin);
        // freopen("P1842.out", "w", stdout);
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i].w >> a[i].s;
            a[i].cnt = a[i].w + a[i].s;
            x += a[i].w;
        }
        sort(a + 1, a + n + 1, cmp);
        for (int i = n; i >= 1; i--)
        {
            x -= a[i].w;
            ans = max(ans, x - a[i].s);
        }
        cout << ans;
        return 0;
    }
    
  • 1

信息

ID
571
时间
1000ms
内存
125MiB
难度
3
标签
递交数
44
已通过
25
上传者