SOJ 1021. Couples

来源:互联网 发布:红包埋雷软件 编辑:程序博客网 时间:2024/04/28 16:25

用栈来实现,然后把1到2n依次压入栈中,如果栈的顶端和即将压入的数互为夫妻的话,就将栈顶端pop出,否则push即将压入的数。

#include<iostream>#include<stack>using namespace std;int main(){int n;while(cin >> n && n != 0){int cp[2*n+1];for (int i=1; i <= n; i++)        {            int a,b;            cin >> a >> b;            cp[a] = b;            cp[b] = a;        }stack<int> fst;for(int i = 1; i <= 2*n; i++){if(!fst.empty() && fst.top() == cp[i]){fst.pop();}else{fst.push(i);}}if(fst.empty()){cout << "Yes" << endl;}else{cout << "No" << endl;}}return 0;} 


0 0
原创粉丝点击