I'm Telling the Truth HDU - 3729

题目传送门

题意:在一次考试之后,给你其中n个学生的考试排名的范围,问这其中最多有多少个可能是正确的。如果人数相等的话输出字典序最大的一组。

思路:比赛的时候看到这个题目第一反应是贪心,但是继续往下就没有了思路,后来发现这个其实是一个二分图匹配,但是自己不知道怎么匹配了,然后就在场上对这个序列正反排序进行贪心(面向样例编程),并没有做出来,场下看了题解发现用匈牙利算法就可以了,还是不难的,只有把匹配的方法想好了就行。然后最后从大到小进行匹配使得字典序尽可能的大。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <list>

#define MAXN 110
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)

using namespace std;

vector<int> vec[MAXN];
vector<int> vector1;
int match[MAXN * 1000];
int check[MAXN * 1000];

bool dfs(int x) {
    for (int i = 0; i < vec[x].size(); ++i) {
        int to = vec[x][i];
        if (!check[to]) {
            check[to] = 1;
            if (match[to] == -1 || dfs(match[to])) {
                match[to] = x;
                //match[x] = to;
                return true;
            }
        }
    }
    return false;
}

int main() {
    std::ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for (int kase = 1; kase <= T; ++kase) {
        memset(match, -1, sizeof(match));
        memset(vec, 0, sizeof(vec));
        int n;
        cin >> n;
        int x, y;
        for (int i = 1; i <= n; ++i) {
            cin >> x >> y;
            for (int j = x; j <= y; ++j) {
                vec[i].push_back(j);
            }
        }
        vector1.clear();
        for (int i = n; i >= 1; --i) {
            memset(check, 0, sizeof(check));
            if (dfs(i))
                vector1.push_back(i);
        }
        sort(vector1.begin(), vector1.end());
        cout << vector1.size() << endl;
        for (int i = 0; i < vector1.size(); ++i) {
            if (i == vector1.size() - 1)
                cout << vector1[i] << endl;
            else
                cout << vector1[i] << " ";
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值