匹配问题_英文题_Guardian of Decency

Title:

Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple: 

  • Their height differs by more than 40 cm. 
  • They are of the same sex. 
  • Their preferred music style is different. 
  • Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).


So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information. 

Input

The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items: 

  • an integer h giving the height in cm; 
  • a character 'F' for female or 'M' for male; 
  • a string describing the preferred music style; 
  • a string with the name of the favourite sport.


No string in the input will contain more than 100 characters, nor will any string contain any whitespace. 

Output

For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.

Sample Input

2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball

Sample Output

3
7

思路:

   同第一篇匹配问题。但是存在一些特殊。

特殊点:

       上一道题目解析(出租车):题目为接客方面的,x既然能够接到y,y又能接到z,在y被匹配的条件下,那么不妨x等一会接z。

       该题目解析:题目为情侣匹配的,x既然可以匹配到y,y又能匹配到z,那么x就能匹配到z,(同性恋,答案错误。此处wa了好多次、z和x假如不是同性恋,还存在x,z的一些判断问题,比如music,sport等等判断)

  修正:

  第一步:人分为两个数组woman,man(去掉同性恋问题)

 第二步:男生找女生,或者女生找男生。我以男生找女生为例;

    x为man数组的,y为woman数组的,假如y有喜欢的了。那么merg[i]就是表示喜欢y的人,如果喜欢y的人还存在一个z(woman)与其之间存在关系而且z并没有匹配掉。那么喜欢y的人就可以找到z,与之匹配。这样x就可以找到y了。

总结:

    题目太坑,二分匹配太难理解。

    x找y,y如果存在喜欢的人,那么就让喜欢y的人,去寻找另外一个人z,找到了y就是x的了,没找到就算了。

 

代码如下:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

struct node
{
    int height;
    char sex[2];
    char music[20];
    char sport[20];
} man[505],woman[505],temp;
int manNum,womanNum;
int n,vis[505],data[505][505],merg[505];
///关系确定的判断
int ok(node n,node m)
{
    if(abs(n.height - m.height) <= 40 && strcmp(n.music,m.music) == 0 && strcmp(n.sport,m.sport) != 0)
        return 1;
    else
        return 0;
}
///模板:二分匹配的模板
int Find(int x)
{
    for(int i=0; i< womanNum; i++)
    {
        if( data[x][i] == 1 && vis[i] == 0)
        {
            vis[i] = 1;
            ///如果i没有伴侣,或者他的伴侣存在另外一条关系。则i时x的了。
            if(merg[i] == -1 || Find(merg[i]))
            {
                merg[i] = x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(data,0,sizeof(data));
        scanf("%d",&n);
        manNum = womanNum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%s%s%s",&temp.height,&temp.sex,&temp.music,&temp.sport);
            if(temp.sex[0] == 'M')
                woman[womanNum++] = temp;
            else
                man[manNum++] = temp;
        }
        ///确定二者的关系
        for(int i=0; i< manNum ; i++)
        {
            for(int j=0; j< womanNum ; j++)
            {
                if(ok(man[i],woman[j]) == 1)
                    data[i][j] = 1;
            }
        }
        memset(merg,-1,sizeof(merg));
        int cnt = 0;
        for(int i=0; i<manNum; i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i))
                cnt++;
        }
        printf("%d\n",n - cnt);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值