You will be given two sets ofintegers. Let�s call them set A and setB.Set Acontains nelements and setB contains m elements. You have to remove k1elements from set A and k2elements from setB so that of the remaining valuesno integer in set B is a multiple of any integer insetA.k1should be in the range [0,n] andk2 in therange [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 integerKsuch 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 fromA and 8from B,we get the sets {4,5} and {6,7,9}.Here none of the integers 6,7 or 9is a multiple of 4 or 5.
So for this case the answer is 3(2from set Aand 1from set B).
Input
The first line of input is aninteger T(T<50) thatdetermine the number of test cases. Each case consists of two lines. The firstline starts withn followed by n integers. The second line startswithmfollowed by m integers. Both n andm will be in therange [1,100].All the elements of the two sets will fit in32 bit signedinteger.
Output
For each case, output thecase number followed by the answer.
Sample Input | Output for Sample Input |
2 4 2 3 4 5 4 6 7 8 9 3 100 200 300 1 150 | Case 1: 3 Case 2: 0 |
题意:给出两个集合A和B,从A,B集合中删除最小的数,使得集合B中没有元素是集合 A元素的倍数
思路:用最大二分匹配,注意0是0的倍数
用匈牙利算法的DFS耗时0.016m
11159 - Factors and Multiples | Accepted | C++ | 0.016 | 0.000 | 277 | 2 hours ag |
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100;
int x[MAXN], y[MAXN];
int g[MAXN][MAXN];
int n, m;
int mx[MAXN], my[MAXN];
bool vis[MAXN];
void input()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &y[i]);
}
}
bool match(int u)
{
for (int i = 0; i < m; i++) {
if (!vis[i] && g[u][i]) {
vis[i] = true;
if (my[i] == -1 || match(my[i])) {
my[i] = u;
mx[u] = i;
return true;
}
}
}
return false;
}
void solve()
{
memset(g, 0x00, sizeof(g));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((x[i] != 0 && y[j] % x[i] == 0) || (x[i] == 0 && y[j] == 0)) g[i][j] = 1;
}
}
memset(mx, 0xff, sizeof(mx));
memset(my, 0xff, sizeof(my));
int ans = 0;
for (int i = 0; i < n; i++) {
if (mx[i] == -1) {
memset(vis, false, sizeof(vis));
if (match(i)) ans++;
}
}
printf("%d\n", ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/cygdrive/d/OJ/uva_in.txt", "r", stdin);
#endif
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
printf("Case %d: ", i);
input();
solve();
}
return 0;
}
用匈牙利算法的BFS耗时0.012s
11159 - Factors and Multiples | Accepted | C++ | 0.012 | 0.000 | 164 | 16 mins ago |
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100;
int x[MAXN], y[MAXN];
int g[MAXN][MAXN];
int n, m;
int mx[MAXN], my[MAXN];
void input()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &y[i]);
}
}
int match()
{
int pre[MAXN];
int ans = 0;
for (int i = 0; i < n; i++) {
if (mx[i] == -1) {
for (int j = 0; j < m; j++) pre[j] = -2;
int q[MAXN];
int front = 0, rear = 0;
int cur;
for (int j = 0; j < m; j++) {
if (g[i][j]) {
pre[j] = -1;
q[rear++] = j;
}
}
while (front < rear) {
cur = q[front];
if (my[cur] == -1) break;
front++;
for (int j = 0; j < m; j++) {
if (pre[j] == -2 && g[my[cur]][j]) {
pre[j] = cur;
q[rear++] = j;
}
}
}
if (front >= rear) continue;
while (pre[cur] > -1) {
mx[my[pre[cur]]] = cur;
my[cur] = my[pre[cur]];
cur = pre[cur];
}
mx[i] = cur; my[cur] = i;
ans++;
}
}
return ans;
}
void solve()
{
memset(g, 0x00, sizeof(g));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((x[i] != 0 && y[j] % x[i] == 0) || (x[i] == 0 && y[j] == 0)) g[i][j] = 1;
}
}
memset(mx, 0xff, sizeof(mx));
memset(my, 0xff, sizeof(my));
int ans = match();
printf("%d\n", ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva_in.txt", "r", stdin);
#endif
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
printf("Case %d: ", i);
input();
solve();
}
return 0;
}