【AOJ 2001】Amida, the City of Miracle
問題概要
- あみだくじの動きをシミュレーションしましょう。
Amida, the City of Miracle | Aizu Online Judge
ソースコード
#include <iostream> #include <utility> #include <vector> #include <algorithm> #define pb push_back #define rep(i,n) for(int i=0;i<n;i++) #define P pair #define pii pair<int,int> using namespace std; int main(){ int n, m, a; while(cin >> n,n){ cin >> m >> a; int h,p,q; vector<P<int,pii> > line; rep(i,m){ cin >> h >> p >> q; line.pb(P<int,pii >(h,pii(p, q))); } sort(line.begin(), line.end(), greater<pair<int,pii> >()); int next_index=0; h = line[0].first; while(h > 0){ for(int i = next_index; line[i].first == h; i++){ if(line[i].second.first == a){ a = line[i].second.second; }else if(line[i].second.second == a){ a = line[i].second.first; } next_index = i+1; } h--; } cout << a << endl; } }
効率のよい書き方がわからなかったので思いついたままに書いてしまった。センスが無い。