标签:
刚开始一直超时,还是没有完全掌握灵活运用数据结构。
才发现用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 }
标签:
原文地址:http://www.cnblogs.com/zmj97/p/5447624.html