2 条题解

  • 1
    @ 2025-1-18 10:05:01

    贪心x

    dp√

    code:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <unordered_map>
    #include <map>
    #include <cstring>
    #include <cmath>
    #include <unordered_set>
    #include <set>
    #include <utility>
    #include <climits>
    
    #define int long long
    #define PII pair<int , int>
    #define inf 0x3f3f3f
    using namespace std;
    
    typedef struct
    {
        int w , s;
    }node;
    
    bool cmp(node a , node b)
    {
        return a.s + a.w < b.w + b.s;
    }
    
    int n;
    
    void solve() 
    {
        cin >> n;
        vector<node> a(n + 1);
        for (int i = 1 ; i <= n ; i ++ ) cin >> a[i].w >> a[i].s;
    
        sort(a.begin() + 1 , a.end() , cmp);
    
        int ans = 0;
        int nowTopWeight = 0;
    
        for (int i = 1 ; i <= n ; i ++ ) 
        {
            int temp = nowTopWeight - a[i].s;
            ans = max(ans , temp);
            nowTopWeight += a[i].w;
        }
    
        cout << ans << endl;
    }
    
    signed main() 
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr), cout.tie(nullptr);
    
        solve();
        return 0;
    }
    
    
    • 0
      @ 2025-1-12 14:58:22
      #include<iostream>
      #include<algorithm>
      #define int long long
      using namespace std;
      typedef long long LL;
      typedef pair<LL, LL> PII;
      const LL N = 50010;
      LL a[N], res;
      PII cow[N];
      signed main()
      {
          LL n;
          cin >> n;
          for (int i = 0; i < n; ++ i)
          {
              int w, s;
              cin >> w >> s;
              cow[i] = { w + s,w };
          }
          sort(cow, cow + n);
          LL sum = 0;
      
          for (int i = 0; i < n; ++ i)
          {
              int s = cow[i].first - cow[i].second;
              int w = cow[i].second;
              res = max(res, sum - s);
              sum += w;
          }
      
          cout << res << endl;
          return 0;
      }
      
      • 1

      信息

      ID
      5526
      时间
      1000ms
      内存
      256MiB
      难度
      10
      标签
      递交数
      5
      已通过
      2
      上传者