HDU 4619 Warm up 2

来源:互联网 发布:淘宝卖毛衣好的店 编辑:程序博客网 时间:2024/05/21 22:27

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 30 00 30 11 11 34 50 10 23 12 20 01 02 04 13 20 0
 

Sample Output
46
 

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));      }  }