sicily 1021. Couples

来源:互联网 发布:js 打印数组 编辑:程序博客网 时间:2024/06/02 05:32

题目地址:http://soj.me/1021

题目的大概意思就是N对夫妇围成一个圈,编号为1-2N,相邻的夫妇可以移除,输入为N对夫妇的编号,判断最终可否移除所有的夫妇。一开始我想模拟整个过程,但是移除夫妇的话,需要做标记或者移动,很是麻烦,后来看到别人的思路用栈实现,边输入,边移除。

#include <iostream>#include <stack>#include <cstring>using namespace std;int array[200001];int main() {  int num;  while (cin >> num) {    if (num == 0)      break;    memset(array, 0, sizeof(array));    for (int i = 0; i < num; i++) {      int a, b;      cin >> a >> b;      array[a] = b; //a的配偶为b       array[b] = a; //b的配偶为a     }    stack<int> st;    for (int i = 1; i <= num*2; i++) {      if (!st.empty() && array[i] == st.top())        st.pop();      else        st.push(i);    }    if (st.empty())      cout << "Yes" << endl;    else      cout << "No" << endl;  }  return 0;}    


0 0
原创粉丝点击