• Acwing 周赛135 解题报告 | 珂学家 | 反悔堆贪心



    前言

    在这里插入图片描述


    整体评价

    VP了这场比赛, T3挺有意思的,反悔贪心其实蛮套路的。


    A. 买苹果

    思路: 签到

    n, x = list(map(int, input().split()))
    print (n // x)
    
    • 1
    • 2

    B. 牛群

    思路: 分类讨论

    from collections import Counter
    
    s = input()
    cnt = Counter(s)
    
    lists = sorted(cnt.values())
    
    n = len(lists)
    if n >= 5:
        print ("No")
    elif n == 4:
        print ("Yes")
    elif n == 3:
        mz = lists[-1]
        if mz >= 2:
            print("Yes")
        else:
            print("No")
    elif n == 2:
        if lists[0] >= 2 and lists[1] >= 2:
            print("Yes")
        else:
            print("No")
    else:
        print("No")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    C. 货运公司

    思路: 反悔堆

    类似这种1vs1匹配,有限制,又求最大收益的题,往往是反悔堆解法。

    当然也可以用网络流,但是反悔的思路更常见。

    这种思路,也属于贪心中的,很特别的存在,有一定的套路在。

    
    #include 
    
    using namespace std;
    
    
    struct T1 {
        int p, w, idx;
        bool operator< (const T1 &other) {
            return this->p < other.p;
        }
    };
    
    struct T2 {
        int p, idx;
        bool operator< (const T2 &other) {
            return this->p < other.p;
        }
    };
    
    struct T3 {
        int w, idx1, idx2;
    };
    
    struct T3Comp {
        bool operator() (const T3 &lhs, const T3 &rhs) const {
            return lhs.w > rhs.w;
        }
    };
    
    int main() {
    
        ios::sync_with_stdio(false);
        cin.tie(nullptr); cout.tie(nullptr);
    
        int n;
        cin >> n;
        vector vi;
        for (int i = 0; i < n; i++) {
            int p, w;
            cin >> p >> w;
            vi.push_back({p, w, i + 1});
        }
        std::sort(vi.rbegin(), vi.rend());
    
        int m;
        cin >> m;
        vector arr(m);
        for (int i = 0; i < m; i++) {
            cin >> arr[i].p;
            arr[i].idx = i + 1;
        }
        std::sort(arr.rbegin(), arr.rend());
    
        priority_queue, T3Comp> pq;
        long long res = 0;
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (j < m && vi[i].p <= arr[j].p) {
                pq.push({vi[i].w, vi[i].idx, arr[j].idx});
                res += vi[i].w;
                j++;
            } else {
                if (!pq.empty() && vi[i].w > pq.top().w) {
                    auto item = pq.top();
                    res -= item.w;
                    pq.pop();
                    res += vi[i].w;
                    pq.push({vi[i].w, vi[i].idx, item.idx2});
                }
            }
        }
        
        cout << pq.size() << " " << res << endl;
        while (!pq.empty()) {
            auto item = pq.top();
            pq.pop();
            cout << item.idx1 << " " << item.idx2 << endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    写在最后

  • 相关阅读:
    机器学习——boosting之GBDT
    动态链接库(八)--二次开发dll
    C语言每日一练——Day01:求最大公约数(三种方法)
    CentOS7服务器用U盘装centos7系统报错解决方案
    StringBuilder,Stringbuffer和String相关面试笔试
    10道小学趣味数学题
    Git使用
    leetcode-279. 完全平方数
    GBASE 8s 自定义存储过程和函数使用
    基于生产数据测试设计、测试回归
  • 原文地址:https://blog.csdn.net/m0_66102593/article/details/136406089