Submission #32110775
Source Code Expand
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
const vi dx4 = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi dy4 = { 0, 1, 0, -1 };
const vi dx8 = { 1, 1, 0, -1, -1, -1, 0, 1 }; // 8 近傍
const vi dy8 = { 0, 1, 1, 1, 0, -1, -1, -1 };
const int INF = 1001001001; const ll INFL = 4004004004004004004LL;
const double EPS = 1e-12; // 許容誤差に応じて調整
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define distance (int)distance
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define YES(b) {cout << ((b) ? "YES\n" : "NO\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
// 手元環境(Visual Studio)
#ifdef _MSC_VER
#include "local.hpp"
// 提出用(gcc)
#else
#define popcount (int)__builtin_popcount
#define popcountll (int)__builtin_popcountll
#define lsb __builtin_ctz
#define lsbll __builtin_ctzll
#define msb(n) (31 - __builtin_clz(n))
#define msbll(n) (63 - __builtin_clzll(n))
#define gcd __gcd
#define dump(...)
#define dumpel(v)
#define input_from_file(f)
#define output_to_file(f)
#endif
#endif // 折りたたみ用
//--------------AtCoder 専用--------------
#include <atcoder/all>
using namespace atcoder;
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;
//----------------------------------------
void WA() {
int n;
cin >> n;
vi p(2 * n), q(2 * n);
cin >> p >> q;
rep(i, 2 * n) {
p[i] -= 1;
q[i] = 2 * n - q[i];
}
dump(p); dump(q);
rep(i, 2 * n) {
if (i % 2 == 0) {
if (p[i] != i) {
if (p[i] == i + 1 && p[i + 1] == i) {
i++;
}
else {
EXIT(-1);
}
}
}
else {
if (p[i] != i) EXIT(-1);
}
}
rep(i, 2 * n) {
if (i % 2 == 0) {
if (q[i] != i) {
if (q[i] == i + 1 && q[i + 1] == i) {
i++;
}
else {
EXIT(-1);
}
}
}
else {
if (q[i] != i) EXIT(-1);
}
}
vc res(2 * n);
rep(i, 2 * n) {
if (i % 2 == 0) {
res[p[i]] = '(';
}
else {
res[p[i]] = ')';
}
}
rep(i, 2 * n) cout << res[i];
cout << endl;
}
void WA2() {
int n;
cin >> n;
vi p(2 * n), q(2 * n);
cin >> p >> q;
--p; --q;
dump(p); dump(q);
vc res(2 * n);
rep(i, n) {
if (p[2 * i] == 2 * i && p[2 * i + 1] == 2 * i + 1
&& q[2 * (n - 1) - 2 * i] == 2 * i && q[2 * (n - 1) - 2 * i + 1] == 2 * i + 1) {
res[2 * i] = '(';
res[2 * i + 1] = ')';
}
else if (p[2 * i] == 2 * i + 1 && p[2 * i + 1] == 2 * i
&& q[2 * (n - 1) - 2 * i] == 2 * i + 1 && q[2 * (n - 1) - 2 * i + 1] == 2 * i) {
res[2 * i] = ')';
res[2 * i + 1] = '(';
}
else {
EXIT(-1);
}
}
rep(i, 2 * n) cout << res[i];
cout << endl;
}
void WA3() {
int n;
cin >> n;
vi p(2 * n), q(2 * n);
cin >> p >> q;
--p; --q;
dump(p); dump(q);
vc res(2 * n);
int i = 0;
while (i < n) {
int p0 = p[2 * i];
int p1 = p[2 * i + 1];
int q0 = q[2 * (n - 1) - 2 * i];
int q1 = q[2 * (n - 1) - 2 * i + 1];
if (p0 == 2 * i) {
dump("aaaa", i);
if (q0 != 2 * i) EXIT(-1);
int d = q1 - q0;
dump(d);
rep(j, 2 * d) {
if (p[2 * i + j] != 2 * i + j) EXIT(-1);
}
rep(j, d) {
if (q[2 * (n - 1) - 2 * (i + j)] != 2 * i + j) EXIT(-1);
if (q[2 * (n - 1) - 2 * (i + j) + 1] != 2 * i + d + j) EXIT(-1);
}
rep(j, 2 * d) {
res[2 * i + j] = (j < d ? '(' : ')');
}
i += d;
}
else if (q1 == 2 * i) {
dump("bbbb", i);
if (p1 != 2 * i) EXIT(-1);
int d = p0 - p1;
dump(d);
rep(j, 2 * d) {
if (q[2 * (n - 1) - 2 * i + 1 - j] != 2 * i + j) EXIT(-1);
}
rep(j, d) {
if (p[2 * (i + j)] != 2 * i + d + j) EXIT(-1);
if (p[2 * (i + j) + 1] != 2 * i + j) EXIT(-1);
}
rep(j, 2 * d) {
res[2 * i + j] = (j < d ? ')' : '(');
}
i += d;
}
else {
EXIT(-1);
}
}
rep(i, 2 * n) cout << res[i];
cout << endl;
}
//【括弧列の正しさ判定】O(n)
/*
* 文字列 s[0..n) が正しい括弧列かを返す.
*/
bool parenthesis_sequenceQ(const vc& s) {
//【方法】
// 括弧文字列 s[0..n) に対して,'(' を +1, ')' を -1 に置き換える操作を行い,
// さらに左から累積和をとったものを acc[0..n] とする.このとき,
// s が正しい括弧列 ⇔ min(acc) = acc[n] = 0
int n = sz(s);
vi acc(n + 1);
rep(i, n) {
int val = 0;
if (s[i] == '(') val = 1;
if (s[i] == ')') val = -1;
if (val == 0) return false;
acc[i + 1] = acc[i] + val;
}
return *min_element(all(acc)) == 0 && acc[n] == 0;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
//【解説 AC】
// 逆問題の構図になっているのだから,順問題を考えて手がかりを掴むところから始めるべきだった.
// サンプルの過半数が -1 なのを見てゆきこでありがちなギャグ系だと思ってしまったのが間違い.
//
// 順問題は,
// 与えられた括弧列 S[0..2n) に対して,
// S[p[0..2n)] が正しい括弧列になるような [0..2n) の順列 p[0..2n) のうち,
// 辞書順で最小のもの P[0..2n) および最大のもの Q[0..2n) を求めよ.
// となる.
//
// 順問題の方であれば,
// 括弧列の典型的な扱い方に則って,'(' → +1, ')' → -1 と置き換えて累積和をとる.
// 辞書順の典型的な扱い方に則って,昇順 or 降順に選べるもの(累積和が非負)を貪欲に選ぶ.
// というように典型の組み合わせで簡単に解ける.
//
// さらに
// 正しい括弧列になってる部分については
// P は 1 ずつ増えていく
// Q は偶数番目,奇数番目だけに注目すると降順
// 正しい括弧列の逆順(あるいは文字反転)になってる部分については
// Q は 1 ずつ減っていく
// P は偶数番目,奇数番目だけに注目すると昇順
// などにも気づけて手がかりにできる.
int n;
cin >> n;
vi p(2 * n), q(2 * n);
cin >> p >> q;
--p; --q;
dump(p); dump(q);
vvi p2(2, vi(n)), q2(2, vi(n));
rep(i, n) {
p2[0][i] = p[2 * i];
p2[1][i] = p[2 * i + 1];
q2[0][i] = q[2 * i];
q2[1][i] = q[2 * i + 1];
}
dumpel(p2); dumpel(q2);
vvi p2s = p2, q2s = q2;
rep(t, 2) {
sort(all(p2s[t]));
sort(all(q2s[t]), greater<int>());
}
dumpel(p2s); dumpel(q2s);
if (p2 != p2s) EXIT(-1);
if (q2 != q2s) EXIT(-1);
vc s(2 * n);
reverse(all(q));
rep(i, n) {
if (p[2 * i] == 2 * i) {
if (p[2 * i + 1] != 2 * i + 1) EXIT(-1);
s[q[2 * i]] += ')';
s[q[2 * i + 1]] += '(';
}
else if (q[2 * i] == 2 * i) {
if (q[2 * i + 1] != 2 * i + 1) EXIT(-1);
s[p[2 * i]] += '(';
s[p[2 * i + 1]] += ')';
}
else {
EXIT(-1);
}
}
reverse(all(q));
vc sp(2 * n), sq(2 * n);
rep(i, 2 * n) sp[i] = s[p[i]];
rep(i, 2 * n) sq[i] = s[q[i]];
dump(sp); dump(sq);
if (!parenthesis_sequenceQ(sp)) EXIT(-1);
if (!parenthesis_sequenceQ(sq)) EXIT(-1);
rep(i, 2 * n) cout << s[i];
cout << endl;
}
Submission Info
| Submission Time |
|
| Task |
C - Bracket and Permutation |
| User |
ecottea |
| Language |
C++ (GCC 9.2.1) |
| Score |
600 |
| Code Size |
10412 Byte |
| Status |
AC |
| Exec Time |
92 ms |
| Memory |
15716 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
600 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt |
| All |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 02-random-001.txt, 02-random-002.txt, 02-random-003.txt, 02-random-004.txt, 02-random-005.txt, 02-random-006.txt, 02-random-007.txt, 02-random-008.txt, 02-random-009.txt, 02-random-010.txt, 02-random-011.txt, 02-random-012.txt, 02-random-013.txt, 02-random-014.txt, 02-random-015.txt, 02-random-016.txt, 02-random-017.txt, 02-random-018.txt, 02-random-019.txt, 02-random-020.txt, 02-random-021.txt, 02-random-022.txt, 02-random-023.txt, 02-random-024.txt, 02-random-025.txt, 03-no-001.txt, 03-no-002.txt, 03-no-003.txt, 03-no-004.txt, 03-no-005.txt, 03-no-006.txt, 03-no-007.txt, 03-no-008.txt, 03-no-009.txt, 03-no-010.txt, 03-no-011.txt, 03-no-012.txt, 03-no-013.txt, 03-no-014.txt, 03-no-015.txt, 03-no-016.txt, 03-no-017.txt, 03-no-018.txt, 03-no-019.txt, 03-no-020.txt, 03-no-021.txt, 03-no-022.txt, 03-no-023.txt, 03-no-024.txt, 03-no-025.txt, 04-hand-001.txt, 04-hand-002.txt, 04-hand-003.txt, 04-hand-004.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00-sample-001.txt |
AC |
8 ms |
3568 KiB |
| 00-sample-002.txt |
AC |
2 ms |
3536 KiB |
| 00-sample-003.txt |
AC |
2 ms |
3612 KiB |
| 02-random-001.txt |
AC |
89 ms |
15600 KiB |
| 02-random-002.txt |
AC |
77 ms |
15600 KiB |
| 02-random-003.txt |
AC |
89 ms |
15580 KiB |
| 02-random-004.txt |
AC |
80 ms |
15716 KiB |
| 02-random-005.txt |
AC |
91 ms |
15640 KiB |
| 02-random-006.txt |
AC |
92 ms |
15672 KiB |
| 02-random-007.txt |
AC |
89 ms |
15684 KiB |
| 02-random-008.txt |
AC |
89 ms |
15576 KiB |
| 02-random-009.txt |
AC |
89 ms |
15636 KiB |
| 02-random-010.txt |
AC |
80 ms |
15548 KiB |
| 02-random-011.txt |
AC |
84 ms |
15596 KiB |
| 02-random-012.txt |
AC |
88 ms |
15672 KiB |
| 02-random-013.txt |
AC |
79 ms |
15544 KiB |
| 02-random-014.txt |
AC |
89 ms |
15632 KiB |
| 02-random-015.txt |
AC |
89 ms |
15636 KiB |
| 02-random-016.txt |
AC |
90 ms |
15656 KiB |
| 02-random-017.txt |
AC |
88 ms |
15672 KiB |
| 02-random-018.txt |
AC |
91 ms |
15672 KiB |
| 02-random-019.txt |
AC |
90 ms |
15636 KiB |
| 02-random-020.txt |
AC |
89 ms |
15672 KiB |
| 02-random-021.txt |
AC |
89 ms |
15716 KiB |
| 02-random-022.txt |
AC |
90 ms |
15640 KiB |
| 02-random-023.txt |
AC |
89 ms |
15628 KiB |
| 02-random-024.txt |
AC |
92 ms |
15584 KiB |
| 02-random-025.txt |
AC |
89 ms |
15628 KiB |
| 03-no-001.txt |
AC |
75 ms |
13772 KiB |
| 03-no-002.txt |
AC |
75 ms |
13776 KiB |
| 03-no-003.txt |
AC |
76 ms |
13684 KiB |
| 03-no-004.txt |
AC |
75 ms |
13780 KiB |
| 03-no-005.txt |
AC |
76 ms |
13780 KiB |
| 03-no-006.txt |
AC |
77 ms |
13760 KiB |
| 03-no-007.txt |
AC |
75 ms |
13784 KiB |
| 03-no-008.txt |
AC |
76 ms |
13760 KiB |
| 03-no-009.txt |
AC |
75 ms |
13704 KiB |
| 03-no-010.txt |
AC |
75 ms |
13652 KiB |
| 03-no-011.txt |
AC |
75 ms |
13704 KiB |
| 03-no-012.txt |
AC |
76 ms |
13716 KiB |
| 03-no-013.txt |
AC |
75 ms |
13768 KiB |
| 03-no-014.txt |
AC |
76 ms |
13816 KiB |
| 03-no-015.txt |
AC |
74 ms |
13776 KiB |
| 03-no-016.txt |
AC |
76 ms |
13784 KiB |
| 03-no-017.txt |
AC |
76 ms |
13680 KiB |
| 03-no-018.txt |
AC |
76 ms |
13780 KiB |
| 03-no-019.txt |
AC |
77 ms |
13780 KiB |
| 03-no-020.txt |
AC |
74 ms |
13732 KiB |
| 03-no-021.txt |
AC |
79 ms |
15652 KiB |
| 03-no-022.txt |
AC |
79 ms |
15596 KiB |
| 03-no-023.txt |
AC |
77 ms |
15656 KiB |
| 03-no-024.txt |
AC |
79 ms |
15656 KiB |
| 03-no-025.txt |
AC |
78 ms |
15640 KiB |
| 04-hand-001.txt |
AC |
2 ms |
3632 KiB |
| 04-hand-002.txt |
AC |
89 ms |
15656 KiB |
| 04-hand-003.txt |
AC |
88 ms |
15716 KiB |
| 04-hand-004.txt |
AC |
75 ms |
13808 KiB |