//隣り合うもの操作系は、木で見る方法があるけど、今回は区間と区間をマージする×N-1回と考えるのが簡単そう。 //a___b -- c___d -- e___f //全然分からない。俺は雰囲気でコードを書いている。 //…a[0] > a[n-1]ならダメは証明できた。逆はOKっぽいけど、未証明。 #include #include #include #define rep(i, n) for(i = 0; i < n; i++) using namespace std; typedef pair P; int n; int a[100000]; stack

stk; int main() { int i; cin >> n; rep(i, n) cin >> a[i]; vector ans; stk.push(P(a[0], a[0])); for (i = 1; i < n; i++) { stk.push(P(a[i], a[i])); while (stk.size() >= 2) { P r = stk.top(); stk.pop(); P l = stk.top(); stk.pop(); if (l.first < r.second) { if (l.first != l.second) ans.push_back(l.second); if (r.first != r.second) ans.push_back(r.first); stk.push(P(l.first, r.second)); } else { stk.push(l); stk.push(r); break; } } } if (stk.size() >= 2) { cout << "No" << endl; return 0; } P last = stk.top(); ans.push_back(last.second); cout << "Yes" << endl; rep(i, ans.size()) { cout << ans[i]; if (i + 1 < ans.size()) cout << " "; } cout << endl; return 0; }