好久没写C/c++了,明天有个小比赛,然后找了一道超水的题来练手,结果很悲剧。。。虽然是1AC,解题书路就是用数组模仿个链表。。。。
/* * ===================================================================================== * * Filename: test.cpp * * Description: * * Version: 1.0 * Created: 04/06/2012 08:09:29 PM * Revision: none * Compiler: gcc * * Author: alfredtofu (Tofu), alfredtofu@gmail.com * Company: * * ===================================================================================== */ #include <iostream> #include <string> #include <stack> #include <cstring> using namespace std; struct Pair { int x; int y; }; int main() { int n; int a, b, c; int next[100002]; cin >> n; while(n != 0) { bool ok = false; memset(next, -1, sizeof(next)); stack<Pair> s; for(int i = 0; i < n; i++) { cin >> a >> b; ok = false; if(a > b) { c = a; a = b; b = c; } if(a + 1 == b) { next[a] = b; ok = true; } else if(next[a + 1] == b - 1){ next[a] = b; ok = true; } if(!ok) { Pair tmp; tmp.x = a; tmp.y = b; s.push(tmp); } else { Pair tmp; while(!s.empty()) { tmp = s.top(); ok = false; if(tmp.x + 1 == tmp.y) { next[tmp.x] = tmp.y; ok = true; }else if(next[tmp.x + 1] == tmp.y - 1) { next[tmp.x] = tmp.y; ok = true; } if(!ok) break; s.pop(); } } } cin >> n; if(s.empty()) cout << "Yes\n"; else cout << "No\n"; } return 0; }