美文网首页
sicily_1021 Couples

sicily_1021 Couples

作者: 我什么都不知道呀 | 来源:发表于2015-10-06 22:51 被阅读83次

    题目

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can't remove a couple any more.

    Can we remove all the couples out of the circle?

    Input

    There may be several test cases in the input file. In each case, the first line is an integer N(1 <= N <= 100000)----the number of couples. In the following N lines, each line contains two integers ---- the numbers of each couple.
    N = 0 indicates the end of the input.

    Output

    Output "Yes" if we can remove all the couples out of the circle. Otherwise, output "No".

    Sample Input

    4
    1 4
    2 3
    5 6
    7 8

    2
    1 3
    2 4

    0

    Sample Output

    Yes
    No

    思路

    用数组模拟堆栈,如果检查到是一堆夫妻在一起,就将这对夫妻出栈,直到把所有夫妻都操作过一遍。最后判断栈顶是否为空即可。

    代码

    #include<stdio.h>
    #include<string.h>
    
    int main() {
      int i, j, n, a, b, c[200000 + 20], d[200000 + 20];
      while (scanf("%d", &n) && n) {
        memset(c, 0, sizeof(c));
        memset(d, 0, sizeof(d));
        for (i = 0; i < n; i++) {
          scanf("%d %d", &a, &b);
          c[a - 1] = 2 * i + 1;
          c[b - 1] = 2 * i + 2;
        }
        for (i = 0, j = 0, d[0] = c[0]; i < 2 * n; i++) {
          if (c[i + 1] != d[j] + 1) {
            d[j + 1] = c[i + 1];
            j++;
          } else {
            d[j] = 0;
            j--;
          }
        }
        printf(!d[0] ? "Yes\n" : "No\n");
      }
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:sicily_1021 Couples

          本文链接:https://www.haomeiwen.com/subject/adxncttx.html