結果
| 問題 |
No.1437 01 Sort
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-06-17 02:02:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,953 ms / 2,000 ms |
| コード長 | 23,384 bytes |
| コンパイル時間 | 5,680 ms |
| コンパイル使用メモリ | 297,588 KB |
| 実行使用メモリ | 9,452 KB |
| 最終ジャッジ日時 | 2025-06-17 02:03:21 |
| 合計ジャッジ時間 | 53,462 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
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 vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
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);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#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##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(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> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
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; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
using mint = modint998244353;
//using mint = static_modint<(int)1e9 + 7>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
int mute_dump = 0;
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif
//【幅優先探索(動的)】O(n + m)(遅い)
/*
* st から到達可能な各頂点への最短距離を格納したリストを返す.
* nxt(s) は s の次に訪れることのできる頂点のリストを返す.
*/
template <class T, class FUNC>
map<T, int> dynamic_BFS(T st, const FUNC& nxt) {
// verify : https://atcoder.jp/contests/abc241/tasks/abc241_f
map<T, int> dist; // st からの最短距離を保持するテーブル
dist[st] = 0;
queue<T> que; // 次に探索する頂点を入れておくキュー
que.push(st);
while (!que.empty()) {
// 未探索の頂点 s を得る.
auto s = que.front(); que.pop();
repe(t, nxt(s)) {
// t が発見済みの頂点なら何もしない.
if (dist.count(t)) continue;
// スタートからの最短距離を確定する.
dist[t] = dist[s] + 1;
// 未探索の頂点として t を追加する.
que.push(t);
}
}
return dist;
/* nxt の定義の雛形
using T = ll;
auto nxt = [&](T s) {
vector<T> res;
return res;
};
auto dist = dynamic_BFS(s, nxt);
*/
}
void zikken() {
int c0 = 4, c1 = 4;
using T = string;
auto nxt = [&](T s) {
vector<T> res;
string t(s);
rotate(t.begin(), t.begin() + 1, t.end());
res.push_back(t);
rotate(s.begin() + 1, s.begin() + 2, s.end());
res.push_back(s);
return res;
};
string ini;
rep(hoge, c0) ini += '0';
rep(hoge, c1) ini += '1';
auto dist = dynamic_BFS(ini, nxt);
map<int, vector<string>> d2s;
for (auto [s, d] : dist) d2s[d].push_back(s);
dumpel(d2s);
exit(0);
}
/*
0: (0,00001111 )
1: (1,00011110 )
2: (2,00111100 )
3: (3,01111000 )
4: (4,01110001 11110000 )
5: (5,01100011 11100001 11100010 )
6: (6,01000111 11000011 11000101 11000110 )
7: (7,10000111 10001011 10001101 10001110 )
8: (8,00010111 00011011 00011101 10010110 10011010 10011100 )
9: (9,00101101 00101110 00110101 00110110 00111001 00111010 10101100 10110100 10111000 )
10: (10,01011001 01011010 01011100 01101001 01101010 01101100 01110010 01110100 11011000 11101000 )
11: (11,00110011 01010011 01010101 01100101 10110001 10110010 11010001 11010010 11010100 11100100 )
12: (12,00100111 00101011 01001011 01100110 10100011 10100101 10100110 10101001 10101010 11001001 11001010 )
13: (13,01001101 01001110 01010110 10010011 10010101 11001100 )
14: (14,10011001 )
*/
void zikken2() {
repi(c0, 1, 10) {
repi(c1, 1, 10) {
using T = string;
auto nxt = [&](T s) {
vector<T> res;
string t(s);
rotate(t.begin(), t.begin() + 1, t.end());
res.push_back(t);
rotate(s.begin() + 1, s.begin() + 2, s.end());
res.push_back(s);
return res;
};
string ini;
rep(hoge, c0) ini += '0';
rep(hoge, c1) ini += '1';
auto dist = dynamic_BFS(ini, nxt);
int d_max = -INF;
for (auto [s, d] : dist) chmax(d_max, d);
cout << d_max << " ";
}
cout << endl;
}
exit(0);
}
/*
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 6 8 10 12 14 16 18 20 22
4 8 11 14 17 20 24 27 30 33
5 10 15 19 23 26 30 35 39 42
6 12 18 23 28 33 37 42 47 54
7 14 21 26 34 40 46 50 56 62
8 16 24 30 38 46 53 60 65 72
9 18 27 35 42 50 61 69 77 82
10 20 30 40 47 56 66 77 86 95
*/
void zikken3() {
repi(c0, 1, 10) {
repi(c1, 1, 10) {
using T = string;
auto nxt = [&](T s) {
vector<T> res;
string t(s);
rotate(t.begin(), t.begin() + 1, t.end());
res.push_back(t);
rotate(s.begin() + 1, s.begin() + 2, s.end());
res.push_back(s);
return res;
};
string ini;
rep(hoge, c0) ini += '0';
rep(hoge, c1) ini += '1';
auto dist = dynamic_BFS(ini, nxt);
int d_max = -INF; string s_max;
for (auto [s, d] : dist) if (chmax(d_max, d)) s_max = s;
dump(c0, c1, ":", d_max, s_max);
}
}
exit(0);
}
/*
1 1 : 1 10
1 2 : 2 101
1 3 : 3 1011
1 4 : 4 10111
1 5 : 5 101111
1 6 : 6 1011111
1 7 : 7 10111111
1 8 : 8 101111111
1 9 : 9 1011111111
1 10 : 10 10111111111
2 1 : 2 100
2 2 : 3 1001
2 3 : 4 10011
2 4 : 5 100111
2 5 : 6 1001111
2 6 : 7 10011111
2 7 : 8 100111111
2 8 : 9 1001111111
2 9 : 10 10011111111
2 10 : 11 100111111111
3 1 : 3 1000
3 2 : 6 01010
3 3 : 8 101001
3 4 : 10 1010011
3 5 : 12 10100111
3 6 : 14 101001111
3 7 : 16 1010011111
3 8 : 18 10100111111
3 9 : 20 101001111111
3 10 : 22 1010011111111
4 1 : 4 10000
4 2 : 8 010010
4 3 : 11 0100110
4 4 : 14 10011001
4 5 : 17 010110110
4 6 : 20 0110110110
4 7 : 24 10110110011
4 8 : 27 101101100111
4 9 : 30 1011011001111
4 10 : 33 10110110011111
5 1 : 5 100000
5 2 : 10 0100010
5 3 : 15 01001100
5 4 : 19 011001100
5 5 : 23 1001100011
5 6 : 26 10011000111
5 7 : 30 011001101110
5 8 : 35 1001101110011
5 9 : 39 10011101110011
5 10 : 42 100111011100111
6 1 : 6 1000000
6 2 : 12 01000010
6 3 : 18 010010100
6 4 : 23 0110001100
6 5 : 28 01100011100
6 6 : 33 100011100011
6 7 : 37 0101011010110
6 8 : 42 10110101101001
6 9 : 47 011100111001110
6 10 : 54 1001110011100111
7 1 : 7 10000000
7 2 : 14 010000010
7 3 : 21 0100100100
7 4 : 26 01010001010
7 5 : 34 011000111000
7 6 : 40 0111000111000
7 7 : 46 10001110000111
7 8 : 50 100011100001111
7 9 : 56 0110100110110110
7 10 : 62 10100110110110011
8 1 : 8 100000000
8 2 : 16 0100000010
8 3 : 24 01001000100
8 4 : 30 100100010001
8 5 : 38 0101001010100
8 6 : 46 01110000111000
8 7 : 53 011100001111000
8 8 : 60 1000011110000111
8 9 : 65 01010101101010110
8 10 : 72 011011001101100110
9 1 : 9 1000000000
9 2 : 18 01000000010
9 3 : 27 010010000100
9 4 : 35 0100010001100
9 5 : 42 01001010010010
9 6 : 50 010101000101010
9 7 : 61 0111000011110000
9 8 : 69 01111000011110000
9 9 : 77 100001111000001111
9 10 : 82 1000011110000011111
10 1 : 10 10000000000
10 2 : 20 010000000010
10 3 : 30 0100100000100
10 4 : 40 01000100011000
10 5 : 47 100011000100001
10 6 : 56 0100100100100110
10 7 : 66 01010100101010100
10 8 : 77 011110000011110000
10 9 : 86 0111100000111110000
10 10 : 95 10000011111000001111
*/
//【最短パス(動的,頂点)】O(n + m)(遅い)
/*
* st から gl までの最短距離を返し(到達不能なら INF),path に最短パス上の頂点の列を格納する.
* nxt(s) は s の次に訪れることのできる頂点のリストを返す.
*/
template <class T, class FUNC>
int dynamic_shortest_path(T st, T gl, const FUNC& nxt, vector<T>* path = nullptr) {
// verify : https://atcoder.jp/contests/abc241/tasks/abc241_f
unordered_map<T, int> dist; // st からの最短距離を保持するテーブル
dist[st] = 0;
unordered_map<T, T> p; // 1 つ手前の頂点を記録しておくテーブル(復元用)
queue<T> que; // 次に探索する頂点を入れておくキュー
que.push(st);
while (!que.empty()) {
// 未探索の頂点 s を得る.
auto s = que.front(); que.pop();
bool end_flag = false;
repe(t, nxt(s)) {
// t が発見済みの頂点なら何もしない.
if (dist.count(t)) continue;
// スタートからの最短距離を確定する.
dist[t] = dist[s] + 1;
p[t] = s;
// ゴールに到着したら終了.
if (t == gl) {
end_flag = true;
break;
}
// 未探索の頂点として t を追加する.
que.push(t);
}
if (end_flag) break;
}
// st から gl まで到達不能の場合
if (!dist.count(gl)) return INF;
int d = dist[gl];
// 必要なら経路復元を行う.
if (path != nullptr) {
*path = vector<T>(d + 1);
T t = gl; int i = d;
while (t != st) {
(*path)[i--] = t;
t = p[t];
}
(*path)[0] = st;
}
return d;
/* nxt の定義の雛形
using T = ll;
auto nxt = [&](T s) {
vector<T> res;
return res;
};
*/
}
void zikken4() {
int c0 = 10, c1 = 7;
using T = string;
auto nxt = [&](T s) {
vector<T> res;
string t(s);
rotate(t.begin(), t.begin() + 1, t.end());
res.push_back(t);
rotate(s.begin() + 1, s.begin() + 2, s.end());
res.push_back(s);
return res;
};
string st;
rep(hoge, c0) st += '0';
rep(hoge, c1) st += '1';
string gl = "01010100101010100";
vector<T> path;
dynamic_shortest_path(st, gl, nxt, &path);
dumpel(path);
exit(0);
}
/*
0: 00000000001111111
1: 00000000011111110
2: 00000000111111100
3: 00000001111111000
4: 00000011111110000
5: 00000111111100000
6: 00001111111000000
7: 00011111110000000
8: 00111111100000000
9: 01111111000000000
10: 01111110000000001
11: 11111100000000010
12: 11111000000000101
13: 11110000000001011
14: 11100000000010111
15: 11000000000101111
16: 10000000001011111
17: 10000000010111110
18: 10000000101111100
19: 00000001011111001
20: 00000010111110010
21: 00000101111100100
22: 00001011111001000
23: 00010111110010000
24: 00101111100100000
25: 01011111001000000
26: 00111110010000001
27: 01111100100000010
28: 01111001000000101
29: 11110010000001010
30: 11100100000010101
31: 11001000000101011
32: 10010000001010111
33: 10100000010101110
34: 11000000101011100
35: 10000001010111001
36: 10000010101110010
37: 00000101011100101
38: 00001010111001010
39: 00010101110010100
40: 00101011100101000
41: 01010111001010000
42: 00101110010100001
43: 01011100101000010
44: 00111001010000101
45: 01110010100001010
46: 01100101000010101
47: 11001010000101010
48: 10010100001010101
49: 10101000010101010
50: 11010000101010100
51: 10100001010101001
52: 11000010101010010
53: 10000101010100101
54: 10001010101001010
55: 00010101010010101
56: 00101010100101010
57: 01010101001010100
58: 00101010010101001
59: 01010100101010010
60: 00101001010100101
61: 01010010101001010
62: 00100101010010101
63: 01001010100101010
64: 00010101001010101
65: 00101010010101010
66: 01010100101010100
*/
//【三分探索(最小値)】O(log(r - l))
/*
* 下に単峰な関数 f:S→T の開区間 (l..r) における最小値が f(x) であるとし,組 {x, f(x)} を返す.
* 関数が下に単峰であるとは,階差の符号変化が - → 0 → + の順であることをいう.
* debug_mode = true にして実行すると手元では下に単峰かどうかチェックしながら全探索する.
*/
template <class S, class T, class FUNC>
pair<S, T> ternary_search_min(S l, S r, const FUNC& f, bool debug_mode = false) {
// verify : https://atcoder.jp/contests/abc279/tasks/abc279_d
Assert(r - l >= 2);
#ifdef _MSC_VER
// 下に単峰かどうか自信がないとき用
if (debug_mode) {
vector<T> val;
repi(x, l + 1, r - 1) val.push_back(f(x));
vector<T> dif;
rep(i, sz(val) - 1) {
auto d = val[i + 1] - val[i];
dif.push_back((d > 0) - (d < 0));
}
if (!is_sorted(dif.begin(), dif.end())) {
cerr << "not unimodal!" << endl;
cerr << val << endl;
exit(1);
}
auto it = min_element(all(val));
return make_pair((S)distance(val.begin(), it), *it);
}
#endif
// 最大値の候補が 1 つしかない場合の例外処理(f(i) が不要なら省略可)
if (r - l == 2) return make_pair(l + 1, f(l + 1));
S m1, m2; T f1, f2;
while (r - l > 2) {
S s = l + r;
m1 = s / 2 - (s % 2 < 0);
m2 = m1 + 1;
f1 = f(m1);
f2 = f(m2);
if (f1 > f2) l = m1;
else r = m2;
}
return make_pair(l + 1, l + 1 == m1 ? f1 : f2);
/* f の定義の雛形
using S = int;
using T = ll;
auto f = [&](S x) {
return T(0);
};
auto [x, fx] = ternary_search_min<S, T>(l, r, f);
*/
}
//【文字列上ジャンプ】
/*
* Jump_on_string(STR s, int C = 26, T a = 'a') : O(n C)
* s[0..n) で初期化する.文字種は a から始まる連続する C 種類とする.
*
* int next(int l, T c, int k = 0) : O(1)
* s[l..n) 内の文字 c の左から k 番目(0-indexed)の位置を返す(なければ n を返す)
*
* int prev(int r, T c, int k = 0) : O(1)
* s[0..r) 内の文字 c の右から k 番目(0-indexed)の位置を返す(なければ -1 を返す)
*
* int count(int l, int r, T c) : O(1)
* s[l..r) 内の文字 c の個数を返す.
*/
template <class STR, class T = remove_reference_t<decltype(declval<STR>()[0])>>
class Jump_on_string {
int n;
int C; int a;
// pos[c] : s[0..n) 内の文字 c がある位置の昇順リスト
vvi pos;
// acc[c][i] : 文字 c が s[0..i) に含まれている個数
vvi acc;
//【備考】
// acc に対してさらに c 方向に累積和をとっておけば,
// 自身より大きい[小さい] 文字に関するなんやかんやも O(1) で処理できるようになる.
public:
// S[0..Mn) = s[0..n)×M で初期化する.文字種は a から始まる連続する C 種類とする.
Jump_on_string(const STR& s, int C = 26, T a = 'a') : n(sz(s)), C(C), a(a), pos(C), acc(C, vi(n + 1)) {
// verify : https://atcoder.jp/contests/abc381/tasks/abc381_e
rep(i, n) {
int c = s[i] - a;
pos[c].emplace_back(i);
rep(c2, C) acc[c2][i + 1] += acc[c2][i] + (c2 == c);
}
}
Jump_on_string() : n(0), C(0), a(0) {}
// s[l..n) 内の文字 c の左から k 番目(0-indexed)の位置を返す(なければ n を返す)
int next(int l, T c, int k = 0) {
// verify : https://atcoder.jp/contests/abc381/tasks/abc381_e
c -= a;
Assert(0 <= c); Assert(c < C);
if (l >= n) return n;
chmax(l, 0);
// K : s[0..n) 内の文字 c の個数
int K = acc[c][n];
// s[0..n) 内の左から k 番目とする.
k += acc[c][l];
if (k >= K) return n;
return pos[c][k];
}
// s[0..r) 内の文字 c の右から k 番目(0-indexed)の位置を返す(なければ -1 を返す)
int prev(int r, T c, int k = 0) {
// verify : https://atcoder.jp/contests/abc381/tasks/abc381_e
c -= a;
Assert(0 <= c); Assert(c < C);
if (r < 0) return -1;
chmin(r, n);
// s[0..n) 内の左から k 番目とする.
k = acc[c][r] - 1 - k;
if (k < 0) return -1;
return pos[c][k];
}
// s[l..r) 内の文字 c の個数を返す.
int count(int l, int r, T c) {
// verify : https://atcoder.jp/contests/abc381/tasks/abc381_e
c -= a;
Assert(0 <= c); Assert(c < C);
chmax(l, 0); chmin(r, n);
if (l >= r) return 0;
return acc[c][r] - acc[c][l];
}
};
ll naive(int n, string s) {
using T = string;
auto nxt = [&](T s) {
vector<T> res;
string t(s);
rotate(t.begin(), t.begin() + 1, t.end());
res.push_back(t);
rotate(s.begin() + 1, s.begin() + 2, s.end());
res.push_back(s);
return res;
};
string st = s;
sort(all(st));
string gl = s;
vector<T> path;
return dynamic_shortest_path(st, gl, nxt, &path);
}
//【ランダム三分探索(最小値)】O(log(r - l))
/*
* 下に単峰な関数 f:S→T の開区間 (l..r) における最小値が f(x) であるとし,組 {x, f(x)} を返す.
* 関数が下に単峰であるとは,階差の符号変化が - → 0 → + の順であることをいう.
* f が下に単峰でなくても運が良ければ正しい {x, f(x)} を返す.
*/
template <class S, class T, class FUNC>
pair<S, T> random_ternary_search_min(S l, S r, const FUNC& f) {
// verify : https://atcoder.jp/contests/arc194/tasks/arc194_c
Assert(r - l >= 2);
static bool first_call = true;
static mt19937 mt;
static uniform_int_distribution<S> rnd;
if (first_call) {
first_call = false;
mt.seed((int)time(NULL));
rnd = uniform_int_distribution<S>((S)0, (S)INFL);
}
S m1 = l + 1 + rnd(mt) % (r - l - 1);
T f1 = f(m1);
while (r - l > 2) {
S m2 = l + 1 + rnd(mt) % (r - l - 1 - 1);
if (m2 >= m1) m2++;
T f2 = f(m2);
if (m1 > m2) {
swap(m1, m2);
swap(f1, f2);
}
if (f1 > f2) {
l = m1;
m1 = m2;
f1 = f2;
}
else {
r = m2;
}
}
return make_pair(l + 1, f1);
/* f の定義の雛形
using S = int;
using T = ll;
auto f = [&](S x) {
return T(0);
};
auto [x, fx] = random_ternary_search_min<S, T>(l, r, f);
*/
}
ll solve(int n, string s) {
auto start = chrono::system_clock::now();
auto s_sorted(s);
sort(all(s_sorted));
if (s == s_sorted) return 0;
s = s + s + s;
dump(s);
Jump_on_string S(s, 2, '0');
int c1 = S.count(0, n, '1');
dump(c1);
ll res = INFL;
auto solve = [&](int l) {
int r = l + c1 - 1;
dump("l:", l, "r:", r);
auto f = [&](int R) {
dump("-----------------------------");
int L = R - n + 1;
dump("L:", L, "R:", R);
int c = S.count(r + 1, R + 1, '1');
int mr = c > 0 ? S.prev(r + 1, '0', c - 1) : r + 1;
int ml = mr - 1;
dump("c:", c, "ml:", ml, "mr:", mr);
int st = n - 1;
while (st < L) st += n;
int gl = r + 1;
dump("st:", st, "gl:", gl);
int posL1 = S.next(L, '1', 0);
int lpL = l - posL1;
int lpR = r - mr + 1;
ll cost = 0;
// st から L へ
if (S.count(posL1, min(st + 2, mr), '0') > 0) lpL--;
if (S.count(mr, st + 2, '1') > 0) lpR--;
cost += st - L + 1;
// R から gl へ
cost += R - gl + 1;
dump("lpL:", lpL, "lpR:", lpR);
cost += (ll)max(lpL, lpR) * n;
dump("cost:", cost);
return cost;
};
//dump(f(14)); exit(0);
// auto [x, fx] = ternary_search_min<int, ll>(r - 1, l + n, f, 1); // not unimodal!
auto [x, fx] = random_ternary_search_min<int, ll>(r - 1, l + n, f);
return fx;
};
//solve(9);
while (1) {
repi(l, n, 2 * n - 1) {
dump("----------------------------- l:", l, "-------------------------------");
auto fx = solve(l);
chmin(res, fx);
auto now = chrono::system_clock::now();
auto msec = chrono::duration_cast<chrono::milliseconds>(now - start).count();
//if (msec >= 100) return res;
if (msec >= 1950) return res;
}
}
return -1;
}
void bug_find() {
#ifdef _MSC_VER
// 合わない入力例を見つける.
mute_dump = true;
mt19937_64 mt((int)time(NULL));
uniform_int_distribution<ll> rnd(0LL, 1LL << 60);
rep(hoge, 1000) {
int n = rnd(mt) % 10 + 2; n = 8;
string s;
rep(i, n) s += "01"[rnd(mt) % 2];
auto res_naive = naive(n, s);
auto res_solve = solve(n, s);
if (res_naive != res_solve) {
cout << "----------error!----------" << endl;
cout << "input:" << endl;
cout << n << endl;
cout << s << endl;
cout << "results:" << endl;
cout << res_naive << endl;
cout << res_solve << endl;
cout << "--------------------------" << endl;
}
}
mute_dump = false;
exit(0);
#endif
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
// bug_find();
// zikken4();
int n; string s;
cin >> n >> s;
dump(naive(n, s)); dump("=======");
auto res = solve(n, s);
EXIT(res);
}