您的位置:首页 > 其它

HDU 4619 Warm up 2

2013-07-27 23:24 316 查看


Warm up 2

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 863 Accepted Submission(s): 407



Problem Description

  Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove
some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.

Input

  There are multiple input cases.

  The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.

Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).

  Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).

  Input ends with n = 0 and m = 0.

Output

  For each test case, output the maximum number of remaining dominoes in a line.

Sample Input

2 3
0 0
0 3
0 1
1 1
1 3
4 5
0 1
0 2
3 1
2 2
0 0
1 0
2 0
4 1
3 2
0 0


Sample Output

4
6


Source

2013 Multi-University Training Contest
2

Recommend

zhuyuanchen520

题意: 有n张横牌,m张竖牌。横牌肯定不相交,竖牌肯定不相交。问 最大的不相交的集合。

思路: 贪心。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

//贪心  如果竖牌不影响任何横牌 就结果加一 (因为竖牌也互不影响)
// 如果影响到一张横牌, 就将该横牌换该竖牌(因为该横牌有可能会影响到其他竖牌, 即将横牌删除)
// 如果印象到2张横牌就进队列

const int V = 1000 + 50;
const int MaxN = 2000 + 5;
typedef struct node {
int x1, y1, x2, y2;
};
node a[V], b[V];
int n, m, ans;
bool visit[V];
bool f(int i, int j) {
if(a[i].x1 == b[j].x1 && a[i].y1 == b[j].y1)
return true;
if(a[i].x1 == b[j].x2 && a[i].y1 == b[j].y2)
return true;
if(a[i].x2 == b[j].x1 && a[i].y2 == b[j].y1)
return true;
if(a[i].x2 == b[j].x2 && a[i].y2 == b[j].y2)
return true;
return false;
}
int main() {
int i, j;
while(scanf("%d%d", &n, &m), n || m) {
for(i = 0; i < n; ++i) {
cin >> a[i].x1 >> a[i].y1;
a[i].x2 = a[i].x1 + 1;
a[i].y2 = a[i].y1;
}
for(i = 0; i < m; ++i) {
cin >> b[i].x1 >> b[i].y1;
b[i].x2 = b[i].x1;
b[i].y2 = b[i].y1 + 1;
}
int front = 0, rear = m;
ans = n;
memset(visit, false, sizeof(visit));
while(front != rear) {
int temp = rear, flag = false;
while(front != temp) {
int sum = 0, index = -1;
for(i = 0; i < n; ++i)
if(!visit[i] && f(i, front)) {
sum++;
index = i;
}
if(sum == 0)
ans++;
else if(sum == 1) {
visit[index] = true;
flag = true;
}
else {
b[rear].x1 = b[front].x1;
b[rear].y1 = b[front].y1;
b[rear].x2 = b[front].x2;
b[rear].y2 = b[front].y2;
rear = (rear + 1) % (m + 10);
}
front = (front + 1) % (m + 10);
}
if(!flag)
break;
}
printf("%d\n", max(ans, m));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: