码迷,mamicode.com
首页 > 其他好文 > 详细

(LightOJ 1149) Factors and Multiples

时间:2016-08-23 16:51:34      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://lightoj.com/volume_showproblem.php?problem=1149

Description
You will be given two sets of integers. Lets call them set A and set B. Set A contains n elements and set B contains m elements. You have to remove k1 elements from set A and k2 elements from set B so that of the remaining values no integer in set B is a multiple of any integer in set A. k1 should be in the range [0, n] and k2 in the range [0, m].

You have to find the value of (k1 + k2) such that (k1 + k2) is as low as possible. P is a multiple of Q if there is some integer K such that P = K * Q.



Suppose set A is {2, 3, 4, 5} and set B is {6, 7, 8, 9}. By removing 2 and 3 from A and 8 from B, we get the sets {4, 5} and {6, 7, 9}. Here none of the integers 6, 7 or 9 is a multiple of 4 or 5.

So for this case the answer is 3 (two from set A and one from set B).

Input
Input starts with an integer T (≤ 50), denoting the number of test cases.

The first line of each case starts with an integer n followed by n positive integers. The second line starts with m followed by m positive integers. Both n and m will be in the range [1, 100]. Each element of the two sets will fit in a 32 bit signed integer.

Output
For each case of input, print the case number and the result.

Sample Input
2
4 2 3 4 5
4 6 7 8 9
3 100 200 300
1 150
Sample Output
Case 1: 3
Case 2: 0

方法:二分匹配,求最大匹配数

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;

#define met(a,b) memset(a,b,sizeof(a))
#define ll long long
#define N 505
int Map[N][N],vis[N],used[N];
int a[N],b[N];
int n,m;
int han(int u)
{
    for(int i=1;i<=m;i++)
    {
        if(!vis[i] && Map[u][i])
        {
            vis[i]=1;
            if(!used[i] || han(used[i]))
            {
                used[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t,con=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);met(Map,0);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                if(b[j]%a[i]==0)
                Map[i][j]=1;
        }
        met(used,0);int sum=0;
        for(int i=1;i<=n;i++)
        {
            met(vis,0);
            sum+=han(i);
        }

        printf("Case %d: %d\n",con++,sum);
    }
    return 0;
}

 

(LightOJ 1149) Factors and Multiples

标签:

原文地址:http://www.cnblogs.com/jun939026567/p/5799484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!