Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997

这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时。

下面n行是人进进出出的时刻,0为进,1为出。让你求是否有合法解。

将合法的进入和出去连边,然后二分匹配就可以了。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 const int N = 1e3 + 5;
 8 struct data {
 9     int a , b;
10     bool operator <(const data &cmp) const {
11         return a < cmp.a;
12     }
13 }xx[N];
14 vector <int> G[N];
15 vector <int> vc;
16 int match[N];
17 bool vis[N];
18
19 bool dfs(int u) {
20     for(int i = 0 ; i < G[u].size() ; ++i) {
21         int v = G[u][i];
22         if(!vis[v]) {
23             vis[v] = true;
24             if(match[v] == -1 || dfs(match[v])) {
25                 match[v] = u;
26                 match[u] = v;
27                 return true;
28             }
29         }
30     }
31     return false;
32 }
33
34 bool hungry() {
35     int res = 0;
36     for(int i = 0 ; i < vc.size() ; ++i) {
37         memset(vis , false , sizeof(vis));
38         if(dfs(vc[i]))
39             res++;
40     }
41     if(vc.size() == res)
42         return true;
43     return false;
44 }
45
46 void solve() {
47     memset(match , -1 , sizeof(match));
48     if(hungry()) {
49         printf("No reason\n");
50         for(int i = 0 ; i < vc.size() ; ++i) {
51             printf("%d %d\n" , xx[match[vc[i]]].a , xx[vc[i]].a);
52         }
53     }
54     else {
55         printf("Liar\n");
56     }
57 }
58
59 int main()
60 {
61     int x , y , n;
62     while(~scanf("%d %d" , &x , &y)) {
63         scanf("%d" , &n);
64         int index = 0;
65         for(int i = 1 ; i <= n ; ++i) {
66             scanf("%d %d" , &xx[i].a , &xx[i].b);
67         }
68         sort(xx + 1 , xx + n + 1);
69         for(int i = 1 ; i <= n ; ++i) {
70             if(xx[i].b) {
71                 vc.push_back(i);
72                 for(int j = 1 ; j < i ; ++j) {
73                     if(!xx[j].b && (xx[i].a - xx[j].a <= y || xx[i].a - xx[j].a >= x))
74                         G[i].push_back(j);
75                 }
76             }
77         }
78         solve();
79     }
80     return 0;
81 }

Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

时间: 2024-08-28 14:41:57

Timus OJ 1997 Those are not the droids you're looking for (二分匹配)的相关文章

Timus OJ 1057 数位dp

http://acm.timus.ru/problem.aspx?space=1&num=1057 1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactlyK different integer degrees of B.

URAL 1997 Those are not the droids you&#39;re looking for

二分图的最大匹配. 每一个$0$与$1$配对,只建立满足时差大于等于$a$或者小于等于$b$的边,如果二分图最大匹配等于$n/2$,那么有解,遍历每一条边输出答案,否则无解. #include<map> #include<set> #include<ctime> #include<cmath> #include<queue> #include<string> #include<vector> #include<cst

Light oj 1149--Factors and Multiples【二分匹配 &amp;&amp; 经典建图】

1149 - Factors and Multiples PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You will be given two sets of integers. Let's call them set A and set B. Set A contains n elements and set B contains m elements. You have to remo

Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子可以共存 阴阳离子都至少需要存在一种 那么可以枚举哪2种离子共存 假设枚举a b 然后找到所有的和a可以共存的阴离子(设为x集合)以及和b共存的阳离子(设为y集合) 现在只需求x集合中和y集合中最多有多少个离子可以共存 这个求最大独立集就行了 枚举所有的a b 取最大值 #include <cstdio> #include

timus 1547. Password Search【题意思路+大数模板】

题目地址传送门:URAL 1547 这道题需要用到大数的很多模板,推荐大家去刷刷! 题目大意:Vova忘记了在Timus OJ上面的密码了,密码是由小写字母(a~z)组成的,他只知道密码长度不大于n位,现在他需要用m台数据处理器对密码进行检索,其中检索顺序需要满足字典序.比如他的密码长度不大于2,那就需要依次检索a,b,..........,y,z,aa,ab,..........,zy,zz.输出每台数据检索器的检索区间,使得总的检索效率可以达到最高. 已知密码的总可能数不少于数据处理器个数.

oj集

by http://www.cnblogs.com/yangqingli/p/4931360.html OnlineJudge大集合 什么是OJ Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序源代码,系统对源代码进行编译和执行,并通过预先设计的测试数据来检验程序源代码的正确性. 一个用户提交的程序在Online Judge系统下执行时将受到比较严格的限制,包括运行时间限制,内存使用限制和安全限制等.用户程序执行的结果将被Online Judge系统捕捉并保存,

OJ大集合、

转载自:传送门 什么是OJ Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序源代码,系统对源代码进行编译和执行,并通过预先设计的测试数据来检验程序源代码的正确性. 一个用户提交的程序在Online Judge系统下执行时将受到比较严格的限制,包括运行时间限制,内存使用限制和安全限制等.用户程序执行的结果将被Online Judge系统捕捉并保存,然后再转交给一个裁判程序.该裁判程序或者比较用户程序的输出数据和标准输出样例的差别,或者检验用户程序的输出数据是否满足

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

oj题目分类

注:网上搜的 第一篇    1001 这个就不用说了吧 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最近点对问题,用分治 1008 简单题 1009 贪心 1010 搜索题,剪枝很关键 1011 1012 简单题 1013 简单题(有个小陷阱) 1014 简单题 1015 可以看作搜索题吧 1016 经典的搜索 1017 简单数学题 1018 简单数学题 1019 简单