結果

問題 No.2740 Old Maid
ユーザー shobonvipshobonvip
提出日時 2024-04-21 00:23:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,613 bytes
コンパイル時間 4,555 ms
コンパイル使用メモリ 272,648 KB
実行使用メモリ 20,428 KB
最終ジャッジ日時 2024-04-21 00:23:44
合計ジャッジ時間 12,043 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 171 ms
20,084 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 2 ms
5,376 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 1 ms
5,376 KB
testcase_64 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int op(int a,int b){
	return min(a, b);
}
int e(){
	return 998244353;
}

int main(){
	int n; cin >> n;
	vector<int> p(n), q(n);
	rep(i,0,n){
		cin >> p[i];
		p[i]--;
		q[p[i]] = i;
	}

	segtree<int,op,e> seg1(n / 2);
	segtree<int,op,e> seg2(n / 2);

	rep(i,0,n){
		if (i % 2 == 0){
			seg1.set(i / 2, p[i]);
		}else{
			seg2.set(i / 2, p[i]);
		}
	}

	map<int, pair<int,int>> kukan;
	map<pair<int,int>, int> kug;
	int cnt = 0;

	priority_queue<pair<pair<int,int>, int>> pq;
	
	auto calc = [&](pair<int,int> t) -> pair<int,int> {
		if (t.first % 2 == 0){
			int x = seg1.prod(t.first / 2, t.second / 2);
			int y = seg2.prod(q[x] / 2, t.second / 2);
			return pair(x, y);
		}else{
			int x = seg2.prod(t.first / 2, t.second / 2);
			int y = seg1.prod(q[x] / 2 + 1, t.second / 2 + 1);
			return pair(x, y);
		}
	};

	auto hant = [&](pair<int,int> a) -> pair<int,int> {
		return pair(-a.first, -a.second);
	};

	auto pqpush = [&](pair<int,int> t) -> void {
		kukan[cnt] = t;
		kug[t] = cnt;
		pq.push(pair(hant(calc(t)), cnt));
		cnt++;
	};

	pqpush(pair(0, n));
	vector<int> ans;

	while(!pq.empty()){
		auto k = pq.top();
		pair<int,int> tmp = hant(k.first);

		pq.pop();
		ans.push_back(tmp.first);
		ans.push_back(tmp.second);

		pair<int,int> h = kukan[k.second];
		int a = q[tmp.first];
		int b = q[tmp.second];
		if (h.first < a){
			pqpush(pair(h.first, a));
		}
		if (b + 1 < h.second){
			pqpush(pair(b + 1, h.second));
		}
		if (a + 1 < b){
			pqpush(pair(a + 1, b));
		}
	}
	rep(i,0,n){
		cout << ans[i] + 1;
		if (i == n-1) cout << '\n';
		else cout << ' ' ;
	}
}

0