sicily_1021 Couples

题目

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 6月8号,我所设计的活动模板-第一期公益活动上线了,在做过无数期线下活动后,终于把活动搬到了线上,有了详细的数据统...
    charmzyn阅读 180评论 0 0
  • 1,健身 定了详细的健身计划,其中关于跳绳。昨天看了一篇文章,是位中年健身男生写的,健身瘦下来之后每天为了保持身材...
    菜菜AN阅读 263评论 0 0
  • 之一平躺 平躺看世界, 世界好象轻飘飘的, 没有一点重量。 就象一只小鸟靠在一棵 树上,等待飞翔。 平躺看生活, ...
    闲不语阅读 149评论 0 2
  • 发动叛乱后的一个月后,陆纳在此时已经攻陷了湘州,又在禄口击破了衡州刺史丁道贵,形势一片大好。 “萧瞎子怎么还不派人...
    旧文字阅读 263评论 0 1