AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

算法问题实战策略 MATCHORDER 贪心

作者: 作者的头像   itdef ,  2019-12-26 19:43:42 ,  所有人可见 ,  阅读 949


5


地址 https://algospot.com/judge/problem/read/MATCHORDER
1.png
解法就是 田忌赛马

当能战胜对手的时候使用最低成本 也就是刚好大于等于对手的最小分数

如果不能胜利 则选择 最低分数

代码如下

#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

const int MAX_N = 150;

vector<int>  ruScore;
vector<int>  koScore;

int n, m;


void solve()
{
    sort(koScore.begin(), koScore.end());
    int total = m;
    int start = 0; int winCount = 0;
    for (int i = 0; i < m; i++) {
        int score = ruScore[i];
        auto it = lower_bound(koScore.begin(), koScore.end(), score);
        if (it == koScore.end()) {
            //没有找到大于等于的 填充一个最小的去匹配
            koScore.erase(koScore.begin());
            total--;
        }
        else {
            koScore.erase(it);
            winCount++;
            total--;
        }
    }

    cout << winCount << endl;
}



int main()
{
    cin >> n;

    while (n--) {
        cin >> m;
        ruScore.clear();
        koScore.clear();
        for (int i = 0; i < m; i++) {
            int t; cin >> t;
            ruScore.push_back(t);
        }
        for (int i = 0; i < m; i++) {
            int t; cin >> t;
            koScore.push_back(t);
        }

        solve();
    }

    return 0;
}


0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息