欢迎找我内推微软

sicily 1021 Couples

刚开始一直超时,还是没有完全掌握灵活运用数据结构。

才发现用STL这么方便!!!

 

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 
 5 int main() {
 6     int n;
 7     while (cin>>n,n!=0) {
 8         int m[2*n+1];
 9         for (int i = 0; i <= 2*n; i++) m[i] = 0;
10         for (int i = 1; i <= n; i++) {
11             int a, b;
12             cin>>a>>b;
13             m[a] = m[b] = i;
14         }
15         stack<int> s;
16         for (int i = 1; i <= 2*n; i++) {
17             if (s.empty()||s.top()!=(m[i]+'0')) s.push(m[i]+'0');
18             else if (!s.empty()||s.top()==(m[i]+'0')) s.pop();
19         }
20         if (s.empty()) cout<<"Yes"<<endl;
21         else cout<<"No"<<endl;
22     }
23     return 0;
24 }      

 

posted @ 2016-04-29 21:32  zmj97  阅读(158)  评论(0编辑  收藏  举报
欢迎找我内推微软