結果

問題 No.930 数列圧縮
ユーザー startcppstartcpp
提出日時 2019-12-12 23:19:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 66,784 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 04:51:26
合計ジャッジ時間 4,164 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 28 ms
4,380 KB
testcase_09 AC 33 ms
4,376 KB
testcase_10 AC 26 ms
4,384 KB
testcase_11 AC 29 ms
4,376 KB
testcase_12 AC 33 ms
4,380 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 20 ms
4,380 KB
testcase_15 AC 39 ms
4,380 KB
testcase_16 AC 39 ms
4,380 KB
testcase_17 AC 40 ms
4,380 KB
testcase_18 AC 39 ms
4,380 KB
testcase_19 AC 39 ms
4,380 KB
testcase_20 AC 39 ms
4,380 KB
testcase_21 AC 39 ms
4,384 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 39 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n;
int a[100000];
stack<P> stk;

int main() {
	int i;
	
	cin >> n;
	rep(i, n) cin >> a[i];
	
	vector<int> 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;
}
0